Related learning recommendations: java basic tutorial
Today, I visited the company as usual. Sit down at your workstation and turn on the computer, "It's another day of moving bricks"
. After thinking about it, I opened Idea skillfully, looked at today's requirements, and started typing the code. Hey, who wrote these codes, how come they appear in my code, and they are still waiting to be submitted. I remember I have never written them, so I looked at them with interest:
A sound came from behind. Because I was thinking about the output, I didn't care about the source of the sound, so I continued. After looking at the code, I came to the conclusion:
polygon() before cal() square.cal(), border = 2 polygon() after cal() square.square(), border = 4复制代码
I thought: That’s it? At least you are a Java development engineer, okay? Although you usually do a lot of work, you still have some basic skills. I couldn't help but feel a little proud~
"Is this your answer? It seems that you are not doing well either"The voice suddenly sounded again, this time I was not calm anymore , Nima! I also thought about this answer in my mind, okay? Who can see it? Moreover, it makes people want to perform a set of Awei's Eighteen Styles.
turned his head with a hint of doubt and anger. Why is there no one? I couldn't tolerate any doubts, "Xiaocai, wake up, why did you fall asleep during working hours?"
Did you fall asleep during working hours? I opened my eyes and looked at my surroundings. It turned out to be a dream, and I breathed a sigh of relief. I looked around and saw the department manager standing in front of me. He was sleeping during working hours. Are you feeling unwell or something? I wrote a bunch of bugs yesterday but didn't correct them, and I submitted some messy stuff today. I think your performance this month is not what I want, and based on your performance, I have to start thinking about it for the department.
Before I could say this sentence, felt in my heart I want to take you home with the flower. I don’t care if it’s real or fake in the bar late at night. Please swing it to your heart’s content and forget about him. You are the most charming. Do you know? The alarm bell rang. I I stood up, my back was slightly wet, my forehead was slightly sweaty, I checked my phone, it was Saturday, 8:30, it turned out to be a dream!
Strange, how could I have such a strange dream? It’s so scary. Then I thought of the part of the code in the dream. Are my results wrong? Based on memory, I typed it out again on the computer, and the results are as follows:
/* polygon() before cal() square.cal(), border = 0 polygon() after cal() square.square(), border = 4 */复制代码
square.cal(), the result of border
is actually 0, not 2. Am I not even good at polymorphism now? In front of your computer and mobile phone, you don’t know if you have come up with the correct answer! Regardless of whether you have it or not, let’s review polymorphism with Xiao Cai!Some friends may be confused about not only the result of
square.cal(), border
square.square(), border = 4 Output the doubts first. Then let’s get started with doubts!
Polymorphism
Polymorphism can not only improve the organization and readability of code, but also create scalable programs. The function of polymorphism is to eliminate the coupling relationship
between types.1. Upward transformation
The object can be used either as its own type or as its base type. This method of treating a reference to an object as a reference to its base type is called -
Upcast
Upward transformation.
public class Animal { void eat() { System.out.println("Animal eat()"); } }class Monkey extends Animal { void eat() { System.out.println(" Monkey eat()"); } }class test { public static void start(Animal animal) { animal.eat(); } public static void main(String[] args) { Monkey monkey = new Monkey(); start(monkey); } }/* OUTPUT: Monkey eat() */复制代码
The
start() method in the above test class receives a reference to
Animal, and naturally can also receive from
Animal's exported class. When calling the
eat() method, the
eat() method defined in
Monkey is naturally used without any type conversion. Because upgrading from
Monkey to
Animal can only reduce the number of interfaces, but not fewer interfaces than
Animal.
A not particularly appropriate analogy:
Your father’s property will be inherited to you, and your property is still yours. In general, your property will not be less than your father’s.
在 test.start()
方法中,定义传入的是 Animal
的引用,但是却传入Monkey
,这看起来似乎忘记了Monkey
的对象类型,那么为什么不直接把test
类中的方法定义为void start(Monkey monkey)
,这样看上去难道不会更直观吗。
直观也许是它的优点,但是就会带来其他问题:Animal
不止只有一个Monkey
的导出类,这个时候来了个pig
,那么是不是就要再定义个方法为void start(Monkey monkey)
,重载用得挺溜嘛小伙子,但是未免太麻烦了。懒惰才是开发人员的天性。
因此这样就有了多态
的产生
方法调用中分为 静态绑定
和动态绑定
。何为绑定:将一个方法调用同一个方法主体关联起来被称作绑定。
静态绑定
:又称为前期绑定。是在程序执行前进行把绑定。我们平时听到"静态"的时候,不难免想到static
关键字,被static
关键字修饰后的变量成为静态变量,这种变量就是在程序执行前初始化的。前期绑定
是面向过程语言中默认的绑定方式,例如 C 语言只有一种方法调用,那就是前期绑定。引出思考:
public static void start(Animal animal) { animal.eat(); }复制代码
在start()
方法中传入的是Animal
的对象引用,如果有多个Animal
的导出类,那么执行eat()
方法的时候如何知道调用哪个方法。如果通过前期绑定
那么是无法实现的。因此就有了后期绑定
。
动态绑定
:又称为后期绑定
。是在程序运行时根据对象类型进行绑定的,因此又可以称为运行时绑定
。而 Java 就是根据它自己的后期绑定机制,以便在运行时能够判断对象的类型,从而调用正确的方法。小结:
Java 中除了 static
和 final
修饰的方法之外,都是属于后期绑定
显然通过动态绑定
来实现多态
是合理的。这样子我们在开发接口的时候只需要传入 基类 的引用,从而这些代码对所有 基类 的 导出类 都可以正确的运行。
其中Monkey
、Pig
、Dog
皆是Animal
的导出类
Animal animal = new Monkey()
看上去不正确的赋值,但是上通过继承,Monkey
就是一种Animal
,如果我们调用animal.eat()
方法,不了解多态的小伙伴常常会误以为调用的是Animal
的eat()
方法,但是最终却是调用了Monkey
自己的eat()
方法。
Animal
作为基类,它的作用就是为导出类建立公用接口。所有从Animal
继承出去的导出类都可以有自己独特的实现行为。
有了多态机制,我们可以根据自己的需求对系统添加任意多的新类型,而不需要重载void start(Animal animal)
方法。
在一个设计良好的OOP程序中,大多数或者所有方法都会遵循start()
方法的模型,只与基类接口同行,这样的程序就是具有可扩展性的,我们可以通过从通用的基类继承出新的数据类型,从而添加一些功能,那些操纵基类接口的方法就不需要任何改动就可以应用于新类。
我们先来复习一下权限修饰符:
Scope | Current class | Use a package | Descendant classes | Other packages |
---|---|---|---|---|
public | √ | √ | √ | √ |
protected | √ | √ | √ | × |
default | √ | √ | × | × |
√ | × | × | × |
The above is the detailed content of These days, if you say you know Java, you have to know polymorphism.. For more information, please follow other related articles on the PHP Chinese website!