Black Horse Programmer __ Polymorphism Tips Summary

Source: Internet
Author: User

------Java Traininglook forward to communicating with you! -------

Inherits extends, does not support multiple inheritance, but supports multiple layers of inheritance


How do you use the functionality of an inheritance system?


To use the system, first look at the description of the parent class of the system, as the parent class defines the system's communist functionality.

By understanding the common features, you can know the basic functions of the system.

Then the system can be used basically.

So, in the case of a specific invocation, what is the object that will create the most subclass?

One is because it is possible that the parent class cannot create an object.

The second is to create subclass objects that can use more functionality, including basic and also unique.



A simple sentence: Check the parent class feature to create a subclass object usage feature.

---------------------------------------------------

1. Polymorphism of the embodiment:

Transition up: The reference to the parent class points to its own subclass object, so that the parent class reference becomes the parent class member, using a method that is the intersection of the child parent class, the method that the subclass overrides after inheriting the parent class

Downward transformation: Forces a reference to the parent class to be transferred to the child class object. Remember that you cannot turn a parent class into a subclass because the parent class itself does not have the content of the child class


Abstract class animal{abstract void Eat ();} Class Cat extends Animal{public void Eat () {System.out.println ("eat Fish");} public void Catchmouse () {System.out.println ("catch Mouse");}} Class Dog extends Animal{public void Eat () {System.out.println ("eat Bones");} Publci void Kanjia () {System.out.pritnln ("eat Bones");}} public static void function (Animal a)//animal a=new Cat (); {a.eat;if (a instanceof cat) {cat c= (cat) A;c.catchmouse ();} else if (a instanceof dog) {Dog c= (dog) A;c.kanjia ();}} Main () {function (cat); function (dog);}


--


Upward transformation

Animal a=new Cat ();

A.eat



Down transformation

Cat C = (cat) A


If (a instanceof type) determines whether an object is inherited or equals a type class

if (This.getclass () ==obj.getclass) Determines whether the type of the object is the same as a class


-----------------------

Interview questions, general development will not appear

Tips:

In polymorphic total flower girl member function features:

At compile time: check if the referenced type belongs to a class that has a calling method. If there is, compile through, if not, compile fails

At run time: Check whether the object belongs to a class that has a calling method

Summary of the simple method is: the member function in the polymorphic call, compile look to the left, run to see the right



Animal a=new Cat ();

A.eat

A.catchmouse

Left: Animal whether there is a Catchmouse method, if not, it is compiled does not pass the

Right: Cat has catchmouse eat method, if any, then use the Cat method, if not the parent class method will be run, then it will fail (because the parent class is generally abstract class)



In polymorphism, the characteristics of member variables:

Both compiled and run, refer to the left (the class to which the reference variable belongs)


Static members, without reference to the right, because the object is not required, so, which class on the left to call a static member or method, is the static member or method of the class, that is, the class static belongs to

---


The polymorphism of the assigned value

Quadrangle Obj=new Square (); the declared variable, obj, is of type quadrangle and the assignment is the actual variable of square type

Quadrangle Obj1=new Parallelogram ();


Suqare newobj= (Square) quaobj; Forcing type conversions


Polymorphic array --Assigning the object of the parent array element to the child class

Quadrangle[] quadrangles=new quadrangle[3];quadrangles[0]=new Square (); quadrangles[1]=new Parallelogram (); for ( Quadrangle Obj:quadrangles) {Obj.draw ();}



Polymorphism of the return type -Returns the initial value of the parent type by defining the method of the parent class type

Public quadrangle Getquadrangle () {quadrangle Tmp=new quadrangle (); return tmp;}


Polymorphism of a method--the parameter used by the parent class method is defined as the parent class member, and the child class member calling this method can also call the parent class method

public void Alldraw (Quadrangle quadrangle) {Quadrangle.draw;} Quadrangle.alldraw (square); Quadrangle.alldraw (parallelogram);



Inherit the parent class quadrangle to Suqare and parallelogram.

It then uses the Quadrangle class format to format the array, then assigns the arguments of the child class object to the object of the parent class, and the draw () method for outputting each element with foreach.

---------------------------------------------------------------------------------

public class Quadrangle{public void Draw () {System.out.println ("draw own graph");}}

--------------------------


public class Square extends quadrangle {public void Draw () {System.out.println ("Square");}}

----------------------------

public class Parallelogram extends quadrangle {public void Draw () {System.out.println ("rectangle");}}



-------------------------------

public class Demo{public static void Main (string[] args) {quadrangle[] quadrangles=new quadrangle[3];quadrangles[0]=new Square (); Quadrangles[1]=new Square (); Quadrangles[2]=new Square (); for (quadrangle Obj:quadrangles) {Obj.draw ()}}}


Black Horse Programmer __ Polymorphism Tips Summary

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.