【问题标题】:How to Call the super class's super method如何调用超类的超方法
【发布时间】:2015-04-20 14:13:54
【问题描述】:
class a
{
void show()
{
}
class b extends a
{
void show()
{
}
class c extends b
{
//how to call show method of class a
}
}

}

有谁知道如何使用 super 关键字从 c 类调用 a 类的方法

【问题讨论】:

  • 请格式化该代码。另外,您使用的是什么语言?假设使用伪代码,我会说“show()”应该被继承到“C”类,所以无论如何它应该是可调用的。假设“A”类中的“show()”是私有的,这取决于语言 - 假设 C#,将无法从“A”类调用它(因为该方法是私有的)。

标签: polymorphism overriding core superclass super


【解决方案1】:
class A {
    void show() {
        System.out.println("A");
    }
}

class B extends A {
    void show() {
        System.out.println("B");
    }
}

class C extends B {
    void show() {
        super.show();
    }
}

The above code will display "B" when class C object is invoked.
In your case, there can be 2 options:
1. C extends A - This will let super.show() in show method of class C display "A".
2. Add super.show() in show method of class B so that show method of class C displays both "B" and "A".

There is no way to call super class methods which are higher than level 1. 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 2010-10-18
    • 2012-12-30
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多