Java 如何从超类调用子类方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19616377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a subclass method from superclass?
提问by Bambax
Java newbie here.
Java新手在这里。
Banana and Apple extend Fruit
(Fruit
might as well be an abstract class since no Fruit will ever be instanciated).
Banana and Apple extend Fruit
(Fruit
也可能是一个抽象类,因为没有 Fruit 会被实例化)。
Banana
and Apple
both have a (wildly different) toString()
method.
Banana
并且Apple
两者都有(完全不同的)toString()
方法。
A method somewhere -- let's call it processFruit(Fruit fruit)
-- accepts Fruit
as a parameter, and then calls the toString()
method of the actual Fruit
received (meaning, either a banana or an apple), for logging purposes. How do I do this?
某处的方法——让我们称之为processFruit(Fruit fruit)
——接受Fruit
作为参数,然后调用toString()
实际Fruit
接收的方法(意思是香蕉或苹果),用于记录目的。我该怎么做呢?
I would like to do fruit.toString() and have it run the Apple.toString() or Banana.toString() method based on what the fruit actually is.
我想做 Fruit.toString() 并让它根据水果的实际情况运行 Apple.toString() 或 Banana.toString() 方法。
Things I'd rather not do (because there are many fruits, not just apples and bananas):
我不想做的事情(因为有很多水果,而不仅仅是苹果和香蕉):
- Have two distinct
processFruit
methods,processFruit(Banana banana)
andprocessFruit(Apple apple)
, because the code is identical - Downcast the received fruit to either Apple or Banana, and then call its
toString()
method
- 有两个不同的
processFruit
方法,processFruit(Banana banana)
和processFruit(Apple apple)
,因为代码是相同的 - 将接收到的水果向下转换为 Apple 或 Banana,然后调用其
toString()
方法
There must be another way but I can't see it.
一定有另一种方式,但我看不到。
采纳答案by Jon Skeet
I would like to do fruit.toString() and have it run the Apple.toString() or Banana.toString() method based on what the fruit actually is.
我想做 Fruit.toString() 并让它根据水果的实际情况运行 Apple.toString() 或 Banana.toString() 方法。
That's exactlywhat will happen automatically, so long as you're really overriding the existing parameterless toString()
method. That's how method overriding works.
这正是会自动发生的事情,只要您真的覆盖了现有的无参数toString()
方法。这就是方法覆盖的工作原理。
If you want to do this for a method other than toString()
(which is declared in Object
) you'd want to create an abstract method in Fruit
, and then implement it in each subclass. You can then call it on any Fruit
instance, and the right implementation will be called.
如果您想对toString()
(在 中声明的Object
)以外的方法执行此操作,您需要在 中创建一个抽象方法Fruit
,然后在每个子类中实现它。然后您可以在任何Fruit
实例上调用它,并且将调用正确的实现。
Full example:
完整示例:
import java.util.*;
abstract class Fruit {
public abstract String getColor();
}
class Banana extends Fruit {
@Override public String getColor() {
return "yellow";
}
}
class Apple extends Fruit {
@Override public String getColor() {
return "green";
}
}
public class Test {
public static void main(String[] args) {
List<Fruit> list = new ArrayList<Fruit>();
list.add(new Banana());
list.add(new Apple());
for (Fruit fruit : list) {
System.out.println(fruit.getColor());
}
}
}
See the inheritance part of the Java tutorialfor more details.
回答by JB Nizet
Just call fruit.toString()
. If the fruit is a Banana, then Banana's toString()
method will be called. If it's an Apple, then Apple's toString()
method will be called. That's what polymorphism is all about.
就打电话fruit.toString()
。如果水果是香蕉,那么香蕉的toString()
方法将被调用。如果是苹果,那么苹果的toString()
方法就会被调用。这就是多态性的全部内容。