且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java多态如何调用子类对象的超类方法

更新时间:2023-11-02 18:00:46

没有.一旦您覆盖了一个方法,那么从外部对该方法的任何调用都将被路由到您的覆盖方法(当然,如果它在继承链的下游再次被覆盖).您只能像这样从自己的重写方法内部调用 super 方法:

no. once you've overridden a method then any invocation of that method from outside will be routed to your overridden method (except of course if its overridden again further down the inheritance chain). you can only call the super method from inside your own overridden method like so:

public String someMethod() {
   String superResult = super.someMethod(); 
   // go on from here
}

但这不是你在这里寻找的.你可以把你的方法变成:

but thats not what youre looking for here. you could maybe turn your method into:

public List<String> getNameAbbreviations() {
   //return a list with a single element 
}

然后在子类中这样做:

public List<String> getNameAbbreviations() {
   List fromSuper = super.getNameAbbreviations();
   //add the 3 letter variant and return the list 
}