简体   繁体   English

Java中的继承和多态

[英]Inheritance and polymorphism in Java

I have 2 classes: Triangle and RightAngledTr . 我有两个类: TriangleRightAngledTr RightAngledTr inherits from Triangle . RightAngledTr继承自Triangle The code is as follows: 代码如下:

The Triangle class: Triangle类:

class Triangle {
    public void draw() { 
        System.out.println(“Base::draw\n“);
    }

    public void computeCentroid(Triangle t) {
        System.out.println Base::centroid\n“);
    }
}

The RightAngledTr class: RightAngledTr类:

class RightAngledTr extends Triangle {
    public void draw() { 
        System.out.println(“RightAngle::draw\n“);
    }

    public void computeCentroid(RightAngled t) {
        System.out.println(RtAngle::centroid\n“);
    }
}

In my Driver program I have the following lines of code: 在我的驱动程序中,我有以下几行代码:

Triangle tr= new RightAngledTr ();
RightAngledTr rtr= new RightAngledTr ();
tr.computeCentroid(rtr);
tr.draw();
rtr.computeCentroid(tr);

The output I expected was: 我预期的输出是:

Base::centroid
Base::draw
Base::centroid

However, the output I got was: 但是,我得到的输出是:

Base::centroid
RightAngle::draw
Base::centroid

My question is, why does tr.computeCentroid(rtr) call the Triangle class' method when tr.draw() calls the RightAngleTr class' method? 我的问题是,当tr.draw()调用RightAngleTr类的方法时,为什么tr.computeCentroid(rtr)调用Triangle类的方法?

It doesn't make sense. 这没有意义。 Liskov substitution principle doesn't seem to apply. Liskov替代原则似乎不适用。

The signatures of the computeCentroid() method are different. computeCentroid()方法的签名是不同的。 You are therefore not overriding, but overloading. 因此,你不是压倒一切,而是超载。

You are calling the method draw on the specialised object. 您正在调用专用对象上的方法绘制。 Even when you wrap it inside the base class you are calling the overridden draw method. 即使将它包装在基类中,也可以调用重写的draw方法。

That's how java works. 这就是java的工作方式。 The draw () method is overridden . draw()方法被覆盖

Have a look at oracle documentation article for understanding the concepts. 查看oracle 文档文章以了解这些概念。

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method. 子类中具有相同签名(名称,加上其参数的数量和类型)和返回类型作为超类中的实例方法的实例方法会覆盖超类的方法。

Triangle (parent) holds the reference of RightAngledTr (child) and draw() method is invoked on child objects. Triangle (父级)包含RightAngledTr (子级)的引用,并且在子对象上调用draw()方法。 It's called overriding feature. 它被称为重写功能。

Childs are type of parents . 孩子是父母的类型 Parent holding child instance invokes child's overridden method. 父保持子实例调用子的重写方法。 If method is not present in child, parent method will be invoked. 如果child中不存在method,则将调用parent方法。

Overriding is redefining base class (parent) behaviour with same signature. 覆盖是使用相同的签名重新定义基类(父)行为。 If you change the signature of base class method in child class, it's called overloading. 如果更改子类中基类方法的签名,则称为重载。 You have done the same for other method : computeCentroid() . 您已对其他方法执行了相同的操作: computeCentroid()

But do not expect the same for same variables declared in both parent and child. 但是对于父和子中声明的相同变量,不要指望相同。

Variables are not overridden. 变量不会被覆盖。 Only methods are overridden . 仅覆盖方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM