Home >Java >JavaBase >These days, if you say you know Java, you have to know polymorphism.

These days, if you say you know Java, you have to know polymorphism.

coldplay.xixi
coldplay.xixiforward
2020-09-15 16:46:242485browse

These days, if you say you know Java, you have to know polymorphism.

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:

These days, if you say you know Java, you have to know polymorphism.
Isn’t this polymorphic? Whoever wrote the test on my computer couldn’t help but wonder.

"Look at the output of this?"

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.

"Who are you?"

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.

"I'm not, I didn't, I don't know why I fell asleep, please listen to my explanation!"

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

is 0, but also why it is not

square.square(), border = 4 Output the doubts first. Then let’s get started with doubts! Polymorphism

In object-oriented programming languages, polymorphism is the third basic feature after data abstraction and inheritance.

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

According to the

Richter substitution principle

: Wherever a base class can appear, a subclass can definitely appear.

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

. Because the parent class is above the child class, the child class must reference the parent class, so it is called

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.

These days, if you say you know Java, you have to know polymorphism.

忘记对象类型

test.start()方法中,定义传入的是 Animal 的引用,但是却传入Monkey,这看起来似乎忘记了Monkey 的对象类型,那么为什么不直接把test类中的方法定义为void start(Monkey monkey),这样看上去难道不会更直观吗。

直观也许是它的优点,但是就会带来其他问题:Animal不止只有一个Monkey的导出类,这个时候来了个pig ,那么是不是就要再定义个方法为void start(Monkey monkey),重载用得挺溜嘛小伙子,但是未免太麻烦了。懒惰才是开发人员的天性。

因此这样就有了多态的产生

2.显露优势

方法调用中分为 静态绑定动态绑定。何为绑定:将一个方法调用同一个方法主体关联起来被称作绑定。

  • 静态绑定:又称为前期绑定。是在程序执行前进行把绑定。我们平时听到"静态"的时候,不难免想到static关键字,被static关键字修饰后的变量成为静态变量,这种变量就是在程序执行前初始化的。前期绑定是面向过程语言中默认的绑定方式,例如 C 语言只有一种方法调用,那就是前期绑定。

引出思考:

public static void start(Animal animal) {
    animal.eat();
}复制代码

start()方法中传入的是Animal 的对象引用,如果有多个Animal的导出类,那么执行eat()方法的时候如何知道调用哪个方法。如果通过前期绑定那么是无法实现的。因此就有了后期绑定

  • 动态绑定:又称为后期绑定。是在程序运行时根据对象类型进行绑定的,因此又可以称为运行时绑定。而 Java 就是根据它自己的后期绑定机制,以便在运行时能够判断对象的类型,从而调用正确的方法。

小结:

Java 中除了 staticfinal 修饰的方法之外,都是属于后期绑定

合理即正确

显然通过动态绑定来实现多态是合理的。这样子我们在开发接口的时候只需要传入 基类 的引用,从而这些代码对所有 基类 的 导出类 都可以正确的运行。

These days, if you say you know Java, you have to know polymorphism.

其中MonkeyPigDog皆是Animal的导出类

Animal animal = new Monkey() 看上去不正确的赋值,但是上通过继承,Monkey就是一种Animal,如果我们调用animal.eat()方法,不了解多态的小伙伴常常会误以为调用的是Animaleat()方法,但是最终却是调用了Monkey自己的eat()方法。

Animal作为基类,它的作用就是为导出类建立公用接口。所有从Animal继承出去的导出类都可以有自己独特的实现行为。

可扩展性

有了多态机制,我们可以根据自己的需求对系统添加任意多的新类型,而不需要重载void start(Animal animal)方法。

在一个设计良好的OOP程序中,大多数或者所有方法都会遵循start()方法的模型,只与基类接口同行,这样的程序就是具有可扩展性的,我们可以通过从通用的基类继承出新的数据类型,从而添加一些功能,那些操纵基类接口的方法就不需要任何改动就可以应用于新类。

失灵了?

我们先来复习一下权限修饰符:

##private√×××
  • public:所有类可见
  • protected:本类、本包和子类都可见
  • default:本类和本包可见
  • private:本类可见

私有方法带来的失灵

复习完我们再来看一组代码:

public class PrivateScope {    private void f() {
        System.out.println("PrivateScope f()");
    }    public static void main(String[] args) {
        PrivateScope p = new PrivateOverride();
        p.f();
    }
}class PrivateOverride extends PrivateScope {    private void f() {
        System.out.println("PrivateOverride f()");
    }
}/* OUTPUT
 PrivateScope f()
*/复制代码

是否感到有点奇怪,为什么这个时候调用的f()是基类中定义的,而不像上面所述的那样,通过动态绑定,从而调用导出类PrivateOverride中定义的f()。不知道心细的你是否发现,基类中f()方法的修饰是private。没错,这就是问题所在,PrivateOverride中定义的f()方法是一个全新的方法,因为private的缘故,对子类不可见,自然也不能被重载。

结论

只有非 private 修饰的方法才可以被覆盖

我们通过 Idea 写代码的时候,重写的方法头上可以标注@Override注解,如果不是重写的方法,标注@Override注解就会报错:

These days, if you say you know Java, you have to know polymorphism.

这样也可以很好的提示我们非重写方法,而是全新的方法。

域带来的失灵

当小伙伴看到这里,就会开始认为所有事物(除private修饰)都可以多态地发生。然而现实却不是这样子的,只有普通的方法调用才可以是多态的。这边是多态的误区所在。

让我们再看看下面这组代码:

class Super {    public int field = 0;    public int getField() {        return field;
    }
}class Son extends Super {    public int field = 1;    public int getField() {        return field;
    }    public int getSuperField() {        return super.field;
    }
}class FieldTest {    public static void main(String[] args) {
        Super sup = new Son();
        System.out.println("sup.field:" + sup.field + " sup.getField():" + sup.getField());

        Son son = new Son();
        System.out.println("son.field:" + son.field + " son.getField:" + son.getField() + " son.getSupField:" + son.getSuperField());
    }
}/* OUTPUT
sup.field:0 sup.getField():1
son.field:1 son.getField:1 son.getSupField:0
*/复制代码

从上面代码中我们看到sup.field输出的值不是 Son 对象中所定义的,而是Super本身定义的。这与我们认识的多态有点冲突。

These days, if you say you know Java, you have to know polymorphism.

其实不然,当Super对象转型为Son引用时,任何域访问操作都将由编译器解析,因此不是多态的。在本例中,为Super.fieldSon.field分配了不同的存储空间,而Son类是从Super类导出的,因此,Son实际上是包含两个称为field的域:它自己的+Super

虽然这种问题看上去很令人头痛,但是我们开发规范中,通常会将所有的域都设置为 private,这样就不能直接访问它们,只能通过调用方法来访问。

static 带来的失灵

看到这里,小伙伴们应该对多态有个大致的了解,但是不要掉以轻心哦,还有一种情况也是会出现失灵的,那就是如果某个方法是静态的,那么它的行为就不具有多态性。

老规矩,我们看下这组代码:

class StaticSuper {    public static void staticTest() {
        System.out.println("StaticSuper staticTest()");
    }

}class StaticSon extends StaticSuper{    public static void staticTest() {
        System.out.println("StaticSon staticTest()");
    }

}class StaticTest {    public static void main(String[] args) {
        StaticSuper sup = new StaticSon();
        sup.staticTest();
    }
}/* OUTPUT
StaticSuper staticTest()
*/复制代码

静态方法是与类相关联,而非与对象相关联

3.构造器与多态

首先我们需要明白的是构造器不具有多态性,因为构造器实际上是static方法,只不过该static的声明是隐式的。

我们先回到开头的那段神秘代码:

These days, if you say you know Java, you have to know polymorphism.

其中输出结果是:

/*
    polygon() before cal()
    square.cal(), border = 0
    polygon() after cal()
    square.square(), border = 4
*/复制代码

我们可以看到先输出的是基类polygon中构造器的方法。

这是因为基类的构造器总是在导出类的构造过程中被调用,而且是按照继承层次逐渐向上链接,以使每个基类的构造器都能得到调用。

These days, if you say you know Java, you have to know polymorphism.

因为构造器有一项特殊的任务:检查对象是否能正确的被构造。导出类只能访问它自己的成员,不能访问基类的成员(基类成员通常是private类型)。只有基类的构造器才具有权限来对自己的元素进行初始化。因此,必须令所有构造器都得到调用,否则就不可能正确构造完整对象。

步骤如下:

  • 调用基类构造器,这个步骤会不断的递归下去,首先是构造这种层次结构的根,然后是下一层导出类,...,直到最底层的导出类
  • 按声明顺序调用成员的初始化方法
  • 调用导出类构造其的主体

打个不是特别恰当的比方:你的出现是否先要有你父亲,你父亲的出现是否先要有你的爷爷,这就是逐渐向上链接的方式

构造器内部的多态行为

有没有想过如果在一个构造器的内调用正在构造的对象的某个动态绑定方法,那么会发生什么情况呢? 动态绑定的调用是在运行时才决定的,因为对象无法知道它是属于方法所在的那个类还是那个类的导出类。如果要调用构造器内部的一个动态绑定方法,就要用到那个方法的被覆盖后的定义。然而因为被覆盖的方法在对象被完全构造之前就会被调用,这可能就会导致一些难于发现的隐藏错误。

问题引索

一个动态绑定的方法调用会向外深入到继承层次结构内部,它可以调动导出类里的方法,如果我们是在构造器内部这样做,那么就可能会调用某个方法,而这个方法做操纵的成员可能还未进行初始化,这肯定就会招致灾难的。

敏感的小伙伴是不是想到了开头的那段代码:

These days, if you say you know Java, you have to know polymorphism.

输出结果是:

/*
    polygon() before cal()
    square.cal(), border = 0
    polygon() after cal()
    square.square(), border = 4
*/复制代码

我们在进行square对象初始化的时候,会先进行polygon对象的初始化,在polygon构造器中有个cal()方法,这个时候就采用了动态绑定机制,调用了squarecal(),但这个时候border这个变量尚未进行初始化,int 类型的默认值为 0,因此就有了square.cal(), border = 0的输出。看到这里,小伙伴们是不是有种拨开云雾见青天的感觉!

这组代码初始化的实际过程为:

  • 在其他任何事物发生之前,将分配给对象的存储空间初始化成二进制的零
  • 调用基类构造器时,会调用被覆盖后的cal()方法,由于步骤1的缘故,因此 border 的值为 0
  • 按照声明的顺序调用成员的初始化方法
  • 调用导出类的构造器主体

不知道下次又会做什么样的梦~

These days, if you say you know Java, you have to know polymorphism.

想了解更多编程学习,敬请关注php培训栏目!

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!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete