From the second week to give up the entry (object-oriented) ...... day.9 .. . . . . Inheritance, rewrite, binding, final;

1, inheritance

 

Use implementation inheritance extends
the parent class which members can be inherited by subclasses?
public members, protected members
can not be inherited: private modification, default modification, construction method

 

 

2, a method override (the override) cover

 


Concept: subclass re-implementation of the parent class

  1. The same method name,
  2. Like the parameter list
  3. The return value of type subclass method should, like supertype or subclass
  4. The method of controlling access subclass visible access modifiers which can not be reduced,
  5. Subclass method declaration throws an exception than the parent class can not declare an exception thrown more than

 

3, Object class

 


Object class is the parent of all classes, there are several general methods is overridden
toString (): The results of this method will be used to return or log output
equals (): determines whether the service logic when a heavy object is write
hashCode ();


Supplementary :

Equals the difference between ==
==: for comparison between the basic data types, Boolean values can not be compared with other categories. Value comparison is
a comparison between a reference type for comparison is the address of
equals: Use only the type of call reference, and by default == the same, but in the actual development will override this method

 

Task class:
1, an entity class declaration Student, attributes; name, age, no (student number), a method override equals
2, the student declare a class operation, there is a method:
from a student array, in accordance with the name and Science No judgment given students the existence of
3 test

 

 

public class Student {
           private String name;
           private int  age ;
           private int  no;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getAge() {
			return age;
		}
		public void setAge(int age) {
			this.age = age;
		}
		public int getNo() {
			return no;
		}
		public void setNo(int no) {
			this.no = no;
		}         
}

 

  

 

 

public class Studentmaker {
  
	 public boolean equals(Student a,Student b){
		
		 if(a.getName().equals(b.getName())){
			 if(a.getNo()==b.getNo()){
				 return true;
			 }else{return false;}		 
		 }		 
		 return false;
	 }
}

  

public class Sudenttest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		      Studentmaker studentmaker=new Studentmaker();
             Student s1=new Student();
             Student s2=new Student();
             s1.setName("suxiao");
             s1.setAge(21);
             s1.setNo(123);
             s2.setName("suxiao");
             s2.setAge(21);
             s2.setNo(123);
             System.out.print(studentmaker.equals(s1, s2));
	}

}

  


4, binding


Downcast and upcast
upcast: to convert from subtype supertype, automatic
downcast: type from the parent to the data type conversion, using casts syntax ()

 

Static (early): at compile time can decide who to visit the property or method static, final member or modified method

Dynamic (late): at run time to decide who to visit the property or method, non-static

 


instanceof keyword


instanceof in Java binary operators, the object is left, the right is the class; when the object is to the right class or subclass of objects created, returns true; otherwise, returns false.
Examples of the class include, for instance, and directly or indirectly, all instances of subclasses itself


instanceof explicitly declare the type of the left and the right operand must be of the same type or the presence of inheritance, that need to be in the same inheritance tree, otherwise it will compile error
using the operator most cases is to subsequent scenes downcast , otherwise known type of security guarantee

 

 

Suggested Use:
1, when the method statement, parameters used as the parent type
2, the return value type of method, using the parent type as much as possible

 

. 5, super
first usage: super call parent class property or method in the conventional method (except static methods) in.
The second Usage: In the constructor subclass, super explicitly call parent class constructor ( )

 

 

6, class initialization sequence

 


When subclass constructor method call will first call the parent class constructor is
executed sequentially:
parent class static block of code
subclass static code block
parent code block
parent class constructor of
code blocks subclass
constructor of a subclass of
object: Learn initialization order

 

 

7,final

 


Usage scenarios and significance:
7.1 modifying variables, represents a constant, once initialized can not be modified
method of local variables: assign an initial value can be declared to be in use before the assignment
attribute class members, you can assign initial values statement, also or can be assigned in the code block construction method

If a final modified reference type variable, it can not be reassigned, but one of the non-final member can be modified

Named final variable is generally all uppercase letters, and use underscores to separate
7.2 modification methods, representation can not be overridden by subclasses
7.3 modified class that represents the class can not be inherited

 

 

Automotive define a Vehicle, the following requirements:
(A) properties include: car brands brand (String type), color color (String Type) and the velocity speed (double type).
(B) providing at least a constructor argument (brand and color requirements may be initialized to any value, the initial value of the speed must be 0).
(C) providing getter / setter methods for the property. Note: The car brand can not be changed once initialized.
(D) method defines a run (), with the car running print statements described features.
Define the test class VehicleTest, create a brand in its main method for "benz", color "black" cars.

public class Car {
        
	private final  String brand;  
	private  String color;  
	private  double speed;  
	
	 public Car(String a,String b){
		 this.brand=a;
		 this.color=b;
		 this.speed=0;
	 }
	public String getBrand() {
		return brand;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public double getSpeed() {
		return speed;
	}
	public void setSpeed(double speed) {
		this.speed = speed;
	}
}

  

public class Cartest {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
           Car car=new Car("benz", "黑色");
           Run run=new Run();
           run.run(car);
	}
}

  

the Run class {public 
       
	 public void RUN (Car C) { 
		 System.out.println (c.getColor () + "a" + c.getBrand () + "to" + c.getSpeed () + "run speed"); 
	 } 
}

  

Sedan category subclass Car (2) defines a Vehicle class requirements are as follows:
(A) has properties passenger car number loader (int type).
(B) providing constructor initializes the class attributes.
(C) redefine the run (), the function described by the car running print statements.
(D) defines the test class Test, to create a brand in its main method for "Honda", color is "red", the number of passenger cars for 2 people

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/suxiao666/p/11342877.html