One, polymorphic "Subclass is the parent class"
1. Why Use polymorphic
Increase program extensibility, maintainability, and code reusability
2, what is polymorphic "P71"
Features that exhibit a variety of morphological capabilities the same implementation interface, using different instances to perform different operations
3. How to use polymorphic
The parameter type of the L method is the parent class
Access modifier return type method name (parent class name Object name) {
.......................................................
}
When calling a method, you can pass the object of the child class
3.1 Parent (Child) class Name object = new subclass name ();
3.2 Method Name (subclass object);
The return type of the 3.3 method is the parent class
Access modifier parent class name Method name ([parameter list]) {
Return parent class object or subclass object;
}
//Subclass, Round Public classCircular extends Graph {PrivateFinalDoublex=3.14;//radius PublicCircular (DoubleR) {super (R); } Public voidGetarea () {DoubleM= (Super.getlen () *x*y); System. out. println ("the radius of the circle is"+super.getlen () +"\ n area of"+m); }
//sub-class, rectangular Public classRectangle extends Graph {Private DoubleWeight//width PublicRectangle (DoubleLenDoubleweight) {super (LEN); This. weight=weight; } Public Doublegetweight () {returnweight; } Public voidGetarea () {DoubleM= (Super.getlen () * This. Getweight ()); System. out. println ("the length of the rectangle is"+super.getlen () +", wide for"+ This. weight+"\ n area of"+m); }
//Parent Class Public Abstract classGraph {Private DoubleLen//side length of graphs PublicGraph (DoubleLen) { This. len=Len; } //Get length Public DoubleGetlen () {returnLen; } //abstract Approach-oriented Public Abstract voidGetarea ();}
4. Conversion between polymorphic Neutron class and parent class
a) conversion of subclasses to parent class (up conversion)
Parent class Object name = new subclass ();//Automatic conversion
The name of the object. Method name ([value]);
Use rule "P72" for upward conversion:
The parent class refers to a subclass object when referred to as up conversion or transformation (upcasting), and is automatically type converted
L refer to a variable through the parent class, called by a subclass overriding (overriding) or inheriting the parent class method "Choice 1, 2"
L reference variable through parent class, cannot call subclass-specific method "Select Question 3"
b) Conversion of the parent class to a subclass (down conversion)--coercion of type conversions, restoring the true face of the object (which seed class is specific)
Parent Class object name = new Subclass ();
Object name. Subclass method ();//Compile Error
should be modified to:
Parent class Object name = new Subclass ();
Subclass sub-Class object = (subclass) object name;
Subclass object. Subclass Method ();
//Parent Class Public Abstract classZhu {PrivateString name;//the names of the two PrivateString Food; PrivateString Nation; /** * @return the name*/ PublicString GetName () {returnname; } PublicString getnation () {returnNation; } PublicZhu (String name,string food,string Nation) { This. name=name; This. food=Food ; This. nation=Nation; } PublicString Getfood () {returnFood ; } Public Abstract voidprint ();} Parent classParent Class
//sub-class Public classYuehan extends zhu{PrivateString habit; PublicYuehan (String name,string habit,string food,string Nation) {super (name,food,nation); This. habit=habit; } PublicString gethabit () {returnhabit; } Public voidprint () {System. out. println ("My name"+super.getname () +", from"+super.getnation () +", like to eat"+ This. Getfood () +", hobbies are playing"+ This. Gethabit ()); }}sub-class
Java object-oriented chapter III