class Super {
public void anotherMethod(String s) {
retValue(s)
}
public String retValue(String s) {
return "Super " + s;
}
}
class Sub extends Super {
public void anotherMethod(String s) {
retValue(s)
}
public String retValue(String s) {
return "Sub " + s;
}
}
if suppose in main,
Super s = new Sub();
s.anotherMethod("Test");
Output will be, Sub Test
谁能帮我告诉我如何使用 main 中给定的序列 获得输出Super Test。
And let me explain why I want this, say I have a class which has method test() and it can be overriden by sub classes, in some cases I want the overriden test() and in some cases I want the test() of super class itself, there are many ways to do this, best suggestions will be helpful.
最佳答案
你为什么要这样做??
多态性的全部要点是调用正确的方法而不需要知道你有哪种实例......
关于java - 如何在调用方法时绕过java中的继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/510976/