在Java中,当有人问:
什么是多态性?
重载或重写是可以接受的答案吗?
我认为事情远不止如此。
如果你有一个抽象基类,它定义了一个没有实现的方法,并且你在子类中定义了这个方法,这仍然是覆盖的吗?
我认为超负荷肯定不是正确的答案。
在Java中,当有人问:
什么是多态性?
重载或重写是可以接受的答案吗?
我认为事情远不止如此。
如果你有一个抽象基类,它定义了一个没有实现的方法,并且你在子类中定义了这个方法,这仍然是覆盖的吗?
我认为超负荷肯定不是正确的答案。
当前回答
重写和重载都用于实现多态性。
你可以在一个类中有一个方法 它在或中被覆盖 更多的子类。这个方法可以 不同的东西取决于哪个 类用于实例化对象。
abstract class Beverage {
boolean isAcceptableTemperature();
}
class Coffee extends Beverage {
boolean isAcceptableTemperature() {
return temperature > 70;
}
}
class Wine extends Beverage {
boolean isAcceptableTemperature() {
return temperature < 10;
}
}
你也可以有一种方法 用两组或多组参数重载。这个方法可以 不同的东西基于 传递的参数类型。
class Server {
public void pour (Coffee liquid) {
new Cup().fillToTopWith(liquid);
}
public void pour (Wine liquid) {
new WineGlass().fillHalfwayWith(liquid);
}
public void pour (Lemonade liquid, boolean ice) {
Glass glass = new Glass();
if (ice) {
glass.fillToTopWith(new Ice());
}
glass.fillToTopWith(liquid);
}
}
其他回答
多态性就其意义而言更有可能……java中的override
这都是关于同一个对象在不同情况下的不同行为(在编程方式中…你可以调用不同的参数)
我认为下面的例子会帮助你理解……虽然它不是纯java代码…
public void See(Friend)
{
System.out.println("Talk");
}
但是如果我们改变参数…行为将会改变…
public void See(Enemy)
{
System.out.println("Run");
}
人(这里的“对象”)是一样的……
Polymorphism is a multiple implementations of an object or you could say multiple forms of an object. lets say you have class Animals as the abstract base class and it has a method called movement() which defines the way that the animal moves. Now in reality we have different kinds of animals and they move differently as well some of them with 2 legs, others with 4 and some with no legs, etc.. To define different movement() of each animal on earth, we need to apply polymorphism. However, you need to define more classes i.e. class Dogs Cats Fish etc. Then you need to extend those classes from the base class Animals and override its method movement() with a new movement functionality based on each animal you have. You can also use Interfaces to achieve that. The keyword in here is overriding, overloading is different and is not considered as polymorphism. with overloading you can define multiple methods "with same name" but with different parameters on same object or class.
表达多态性最清晰的方法是通过抽象基类(或接口)
public abstract class Human{
...
public abstract void goPee();
}
这个类是抽象的,因为goPee()方法对人类是不可定义的。它只对子类Male和Female可定义。此外,人是一个抽象的概念——你不能创造一个既不是男性也不是女性的人。一定是两者之一。
因此,我们通过使用抽象类来延迟实现。
public class Male extends Human{
...
@Override
public void goPee(){
System.out.println("Stand Up");
}
}
and
public class Female extends Human{
...
@Override
public void goPee(){
System.out.println("Sit Down");
}
}
现在我们可以让一屋子的人去尿尿了。
public static void main(String[] args){
ArrayList<Human> group = new ArrayList<Human>();
group.add(new Male());
group.add(new Female());
// ... add more...
// tell the class to take a pee break
for (Human person : group) person.goPee();
}
运行该命令会得到:
Stand Up
Sit Down
...
没有:
重载是指使用相同的函数名,但接受不同的参数。
重写是指子类用自己的方法替换父类的方法(这本身不构成多态性)。
多态性是后期绑定,例如,基类(父类)方法被调用,但直到运行时应用程序才知道实际对象是什么——它可能是一个方法不同的子类。这是因为任何子类都可以在定义基类的地方使用。
在Java中,你可以在集合库中看到很多多态性:
int countStuff(List stuff) {
return stuff.size();
}
List是基类,编译器不知道你计数的是链表、向量、数组还是自定义列表实现,只要它像List一样:
List myStuff = new MyTotallyAwesomeList();
int result = countStuff(myStuff);
如果你超载了,你会:
int countStuff(LinkedList stuff) {...}
int countStuff(ArrayList stuff) {...}
int countStuff(MyTotallyAwesomeList stuff) {...}
etc...
编译器会选择countStuff()的正确版本来匹配参数。
你说得对,超载不是解决问题的办法。
两者都不是最重要的。覆盖是获得多态性的方法。多态性是对象根据其类型改变行为的能力。当显示多态的对象的调用者不知道该对象是什么具体类型时,最能说明这一点。