在Java中,当有人问:

什么是多态性?

重载或重写是可以接受的答案吗?

我认为事情远不止如此。

如果你有一个抽象基类,它定义了一个没有实现的方法,并且你在子类中定义了这个方法,这仍然是覆盖的吗?

我认为超负荷肯定不是正确的答案。


当前回答

多态性简单来说就是“多种形式”。

它不需要继承来实现…作为接口实现,它根本不是继承,服务于多态需求。可以说,接口实现比继承更能满足多态需求。

例如,你会创建一个超类来描述所有会飞的东西吗?我不这么认为。你最好创建一个描述飞行的界面,然后就这样吧。

因此,由于接口描述行为,而方法名描述行为(对程序员来说),将方法重载视为一种较小形式的多态性并不过分。

其他回答

重载是指定义两个名称相同但参数不同的方法

重写是通过子类中具有相同名称的函数更改基类的行为。

所以多态性与覆盖有关,但不是真正的重载。

然而,如果有人对“什么是多态性?”这个问题给出了一个简单的“重写”的答案。我要求进一步解释。

多态性是类实例的一种能力,它的行为就像它是其继承树中另一个类的实例一样,通常是它的祖先类之一。例如,在Java中,所有的类都继承自Object。因此,您可以创建Object类型的变量,并将任何类的实例分配给它。

An override is a type of function which occurs in a class which inherits from another class. An override function "replaces" a function inherited from the base class, but does so in such a way that it is called even when an instance of its class is pretending to be a different type through polymorphism. Referring to the previous example, you could define your own class and override the toString() function. Because this function is inherited from Object, it will still be available if you copy an instance of this class into an Object-type variable. Normally, if you call toString() on your class while it is pretending to be an Object, the version of toString which will actually fire is the one defined on Object itself. However, because the function is an override, the definition of toString() from your class is used even when the class instance's true type is hidden behind polymorphism.

重载是定义具有相同名称但具有不同参数的多个方法的操作。它与覆盖或多态性无关。

重写和重载都用于实现多态性。

你可以在一个类中有一个方法 它在或中被覆盖 更多的子类。这个方法可以 不同的东西取决于哪个 类用于实例化对象。

    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中,你可以在集合库中看到很多多态性:

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()的正确版本来匹配参数。

表达多态性最清晰的方法是通过抽象基类(或接口)

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
...