Think in Java (v): polymorphic

Source: Internet
Author: User

1. Dynamic binding
Also known as "late binding" or "runtime binding," it means to determine the type of an object at run time, thus invoking the appropriate method
public class Shapes {private static Randomshapegenerator Gen = new Randomshapegenerator ();p ublic static void Main (string[] args) {shape[] Shapearr = new shape[9];//fill array for (int i = 0; i < shapearr.length; i++) {Shapearr[i] = Gen.next ();} Dynamic call method for (Shape Shape:shapearr) {Shape.draw ();}}} Class Randomshapegenerator {private Random rand = new random;p ublic Shape next () {switch (Rand.nextint (3)) {DEFAULT:CA Se 0:return new Circle (), Case 1:return New Square (), Case 2:return new Triangle ();}} Class Shape {public void Draw () {System.out.println ("Shape.draw ()");} public void Erase () {System.out.println ("shape.erase ()");}} Class Circle extends Shape {public void Draw () {System.out.println ("Circle.draw ()");} public void Erase () {System.out.println ("circle.erase ()");}} Class Triangle extends Shape {public void Draw () {System.out.println ("Triangle.draw ()");} public void Erase () {System.out.println ("triangle.erase ()");}} Class Square extends Shape {public void Draw () {System.out.println ("SquarE.draw () ");} public void Erase () {System.out.println ("square.erase ()");}} /* Output:Triangle.draw () Triangle.draw () Square.draw () Triangle.draw () Square.draw () Triangle.draw () Square.draw () Triangle.draw () Circle.draw () *///:~

2. "Overwrite" the Private method:
The output we expect is public f (), but since the private method is automatically considered the final method and the subclass is masked, in this case
The F () method in the derived class is a completely new method;
Conclusion: Only non-private methods can be overridden, so in subclasses, it is best to use a different name for the private method in the parent class.

public class Privateoverride {private void F () {print ("Private f ()");} public static void Main (string[] args) {privateoverride po = new Derived ();p o.f ();}} Class Derived extends Privateoverride {public void F () {print (' public f () ')}}/* Output:private f () *///:~

3. Constructors and polymorphism:
Complex object Call constructors follow the following order:
1) Call the parent class constructor (if there are multiple layers, it will continue to recursively)
2) Initialize method for calling members in declaration order
3) Call the body of the subclass constructor
Class Meal {Meal () {print ("Meal ()");}} Class Bread {Bread () {print ("Bread ()");}} Class Cheese {Cheese () {print ("Cheese ()");}} Class Lettuce {lettuce () {print ("lettuce ()");}} Class Lunch extends Meal {Lunch () {print ("Lunch ()");}} Class Portablelunch extends Lunch {portablelunch () {print ("Portablelunch ()");}} public class Sandwich extends Portablelunch {private Bread Bread = new Bread ();p rivate Cheese Cheese = new Cheese ();p Rivat E Lettuce lettuce = new lettuce ();p ublic Sandwich () {print ("Sandwich ()");} public static void Main (string[] args) {new Sandwich ();}} /* Output:meal () Lunch () Portablelunch () Bread () Cheese () lettuce () Sandwich ()

4. When writing the constructor, you should make the object as simple as possible and avoid invoking other methods if you can.
In the following example we will find the radius value is 0, why?
A dynamically bound method call is deep inside the inheritance hierarchy, and it can invoke methods of the subclass, but the members manipulated by the method are not initialized yet.

Class Glyph {void Draw () {print ("Glyph.draw ()");} Glyph () {print ("Glyph () before Draw ()"), This.draw ();p rint ("Glyph () after draw ()");}} Class Roundglyph extends Glyph {private int radius = 1; Roundglyph (int r) {radius = R;print ("roundglyph.roundglyph (), radius =" + radius);} void Draw () {print ("Roundglyph.draw (), radius =" + radius);}} public class Polyconstructors {public static void main (string[] args) {new roundglyph (5)}}/* Output:glyph () before Draw ( ) Roundglyph.draw (), radius = 0Glyph () after draw () roundglyph.roundglyph (), radius = 5*///:~

The call is a subclass of the draw method, the meaning of this is who calls me, this is who the glyph constructor is triggered by the super () method in the Roundglyph class constructor, so this point is the draw method in Roundglyph


5. "Transformation up" and "downward transformation"
Upward transformation: List List = new ArrayList (); List.add ("xxx") is actually called ArrayList method at this time.
Note: The newly added method in the subclass cannot be accessed after an upward transformation

Downward transformation: Actually is a strong turn, if we know it is a certain type, otherwise the runtime will be reported ClassCastException

Think in Java (v): polymorphic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.