Home > Back-end >  Java review chapter 3
Java review chapter 3

Time:04-16


Example: in life
Person: the Person: public class Person
Student: the Student Student: public class Student extends the Person
Worker: the Worker Worker: public class Worker extends the Person
Man is a Person of p=new Person ();
Students is one of the people ( polymorphic ) Person p=new Students ();
Workers is one of the people ( polymorphic ) Person p=new Worker ();
Polymorphism between interface and implementation classes Myinter m=new MyinterImpl ();
 
//define the superclass
Public classs Fu {
Public void show () {
System. The out. Println (" of the parent class method ");
}
}

//define subclasses
Public class Zi extends Fu {
//rewrite the superclass method
@ Override
Public void show () {
System. The out. Println (" subclasses override the superclass method ");
}
}

Public class DemoPolymorphsic {
Public static void main (String [] args) {
Fu f=new Fu ();
F.s how ();
}
}


Advantages and disadvantages of polymorphism of
Do not use the polymorphism: benefits is the use of the subclass characteristic method, the disadvantages of poor ductility
Using polymorphism: in contrast to not use polymorphic
polymorphic transformation (up/down)
 
Public class Fu {
Public void the work () {
System. The out. Println (" father at work ");
}
}

//define a subclass
Public class Zi {
@ Override
Public void the work () {
System. The out. Println (" the son in the work ");
}

//subclass peculiar method
Public void playGame () {
System. The out. Println (" son playing ");
}
}
/* *
* upcasting: polymorphism is itself up transformation, the subclass object is assigned to the variables of the parent
* format: the parent class type variable name=new subclass object ();
* benefits: strong scalability, can assign different subclass object
* downward transition: the parent class type strong into subclasses (turn)
* format: subclass type variable name=(subclass type) the superclass variable names;
* benefits: strong transfer is completed, the polymorphism into subclasses type, you can use a subclass peculiar function
*
*/
Public class Demo {
Public static void main (String [] args) {
Fu f=new Zi ();
F.w ork ();

//down transition
Zi z=f (Zi);
Z.p layGame ();//subclass methods unique
}
}


Note: the turn is likely to occur in the process of abnormal ClassCastException: Dog always be cast to...
You can use first instanceof keywords do a judge whether an object belongs to a certain data type

 
Animal a=new Dog ();
A.e the at ();
A. ano ();
If (a instanceof Cat) {//in front of the strong turn made a judgment instanceof
The Cat c=(Cat) a;
}

anonymous inner class
 
//don't use an anonymous inner class
Public abstract class Animal {
Public abstract void eat ();
Public abstract void sleep ();
}

Public class Cat extends Animal {
@ Override
Public void eat () {
System. The out. Println (" eat ");
}

@ Override
Public void sleep () {
System. The out. Println (" sleep ");
}

//the cat characteristic function
Public void catchMouse () {
System. The out. Println (" catch mice ");
}
}

/* *
* an anonymous inner class:
* anonymous: create an inner class, not assigned to a variable that has no name
* internal classes: is a local inner classes (write) in the method
* how it works: simplify the code
* the subclass inherits parent and rewriting of the parent class method, and create a subclass object, synthetic step to complete the
* the implementation class implements the interface and rewrite the interface methods and create the implementation class object, the synthetic step to complete the
* format:
* new parent class/interface () {
* override the parent class/interface methods in the
*}
*/
New Animal () {
@ Override
Public void eat () {
System. The out. Println (" eat ");
}

@ Override
Public void sleep () {
System. The out. Println (" sleep ");
}
}. Eat ();








  • Related