Java Object-oriented learning as much as three characteristics of state

Java is the object-oriented three characteristics: inheritance, encapsulation, polymorphism. The first two understand a little better, polymorphism is relatively difficult to look at, but more practice with the code, and more to think about, is understandable.

Java Object-oriented learning as much as three characteristics of state

What is the first multi-state?

Polymorphism refers to the call the same method, there may be different due to the different behavior of the object. For example in life, like on the rest of the method, the method rest slag slag-hui is fun to play Blue Moon, rest methods Xiao Ming is sleeping, white is tourism, rest Sicong method is to accompany his girlfriend to have sex things another example method it ......... dinner, we Chinese people use chopsticks, they use a knife fork spoon foreigners, India Asan even more wonderful to use the clutch ....

Note polymorphic 3:00

1, is a multi-state polymorphic method, polymorphism and properties are not a cent relationship.

2, the presence of polymorphic three necessary conditions:

(1), you have to have inheritance

(2), subclasses must override the parent's method

(3), the references to the parent class subclass object:

Java Object-oriented learning as much as three characteristics of state

 

3, a reference point to the parent class subclass object, the method is called a subclass overrides with reference to the parent class, then multi-state arises.

Here we test the code with a relatively common parent class as a reference the case of the method parameter:

Java Object-oriented learning as much as three characteristics of state

 

Execution results are as follows:

Java Object-oriented learning as much as three characteristics of state

 

Analysis: The above example may be well understood, the parent class's methods do reference parameter, the argument may be any subclass object may be achieved in different ways by different subclasses behavior of an object.

We pass the parameter type is the type of wine, but when we create the object is a reference point to the object type of wine its subclasses, such as: a = process Maotai liquor, process parameter types passed as if we drink, suppose we are wine master, able to drink out what brand of wine, when we did not drink before the judge know only through the nose the wine, do not know what wine to drink one to know. Likewise, the conversion into the program, compile time parameter type only know the type of wine, run time (drink it) just to know specifically what type to invoke the corresponding method (know what brand of wine).

If there is no polymorphism, we here need to write a lot of overloaded methods, very troublesome, thus, we can see that polymorphic main advantage is to increase the scalability of the code, in line with the principle of opening and closing. But polymorphism also has drawbacks, that can not be called a subclass-specific features, such as, I can not use reference variable to call the parent class Wuliangye liquor unique to say () method.

We want to use that subclass-specific method how to break it? Transition objects can be broken

note:

Supertype reference objects to sub-type, called the upward transition, are automatic type conversion

After the upward transition parent type reference variable, it compiles the type (wine) method can only be called, can not call type (drink taste a specific wine) it will be run method, if you want to use, it must downcast. (Strong turn almost casts, and basic data types)

Object transformation:

Java Object-oriented learning as much as three characteristics of state

 

Operating results as follows:

Java Object-oriented learning as much as three characteristics of state

 

When downcast must pay attention to the problem, that is, the conversion must be run-time type, otherwise there will be cast abnormal ClassCastException.

Java Object-oriented learning as much as three characteristics of state

 

problem appear:

Java Object-oriented learning as much as three characteristics of state

 

So, we can use the instanceof operator to do the conversion operation again,

Java Object-oriented learning as much as three characteristics of state

 

 Editor:

https://www.toutiao.com/i6733474833176199693/?tt_from=weixin&utm_campaign=client_share&wxshare_count=1&timestamp=1584581198&app=news_article&utm_source=weixin&utm_medium=toutiao_android&req_id=202003190926380101290482272665C00C&group_id=6733474833176199693

Guess you like

Origin www.cnblogs.com/diandianquanquan/p/12522152.html
Recommended