Java object-oriented —- polymorphism summary

1. Polymorphism: the ability of the same behavior to have multiple different forms or forms.

3. Three necessary conditions for polymorphism:

There are parent classes and child classes, and the properties of the parent class are obtained

The parent class has and the child class does not. What is obtained is the attribute of the parent class

No parent class, no child class, unable to get

No parent class, no child class, unable to get

Note: see the left for compilation and the left for operation

After the compiler compiles, the type is determined, but the object cannot be determined. Only when running, the new object is what object

There are parent classes and subclasses, and the methods of subclasses are obtained (method overrides)

The parent class has and the child class does not. What is obtained is the method of the parent class

No parent class, no child class, unable to get

No parent class, no child class, unable to get

Note: see the left for compilation and the right for operation (method rewriting)

Manifestation of polymorphism:

Upward Transformation: the reference of the parent class points to the object of the child class

Parent f = new child ();

Downward Transformation: force the subclass object executed by the parent class reference to the subclass type

Sub z = (sub) f;

Note: whether it is a basic type or a reference type, the conversion from small to small is mostly automatic, and the conversion from large to small is mandatory,

Note: the more abstract the type is, the less the function is. The more specific the type is, the more powerful the function is

Note: when converting between reference types, there must be an inheritance relationship

7. Type conversion

(1) : automatic transformation upward transformation

(2) : forced transition down

The parent object becomes a child object,

Class instance, mandatory

Polymorphism is often used in actual development. Creating subclass objects and using polymorphism as much as possible can improve the flexibility and scalability of the code

Title Requirements:

Write a program to make pizza. Write the program of requirement description, receive the information input by the user, and select the pizza to be made. The pizza choices are bacon pizza and seafood pizza.

Implementation ideas and key codes

(1) Analysis of bacon pizza and seafood pizza

(2) Define pizza class

(3) Attribute: name, price, size

(4) Method: display

(5) Define bacon pizza and seafood pizza inherited from the pizza class

(6) Define the pizza factory class and generate specific pizza objects according to the input information

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>