在Java中,当有人问:
什么是多态性?
重载或重写是可以接受的答案吗?
我认为事情远不止如此。
如果你有一个抽象基类,它定义了一个没有实现的方法,并且你在子类中定义了这个方法,这仍然是覆盖的吗?
我认为超负荷肯定不是正确的答案。
在Java中,当有人问:
什么是多态性?
重载或重写是可以接受的答案吗?
我认为事情远不止如此。
如果你有一个抽象基类,它定义了一个没有实现的方法,并且你在子类中定义了这个方法,这仍然是覆盖的吗?
我认为超负荷肯定不是正确的答案。
当前回答
多态是指多个形式,同一对象根据需求执行不同的操作。
多态可以通过以下两种方式实现
方法重写 方法重载
方法重载是指使用相同的方法名在同一个类中编写两个或多个方法,但传递的参数不同。
方法重写意味着我们在不同的类中使用方法名,这意味着父类的方法在子类中使用。
在Java中,为了实现多态,父类引用变量可以容纳子类对象。
为了实现多态,每个开发人员必须在项目中使用相同的方法名。
其他回答
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.
没有:
重载是指使用相同的函数名,但接受不同的参数。
重写是指子类用自己的方法替换父类的方法(这本身不构成多态性)。
多态性是后期绑定,例如,基类(父类)方法被调用,但直到运行时应用程序才知道实际对象是什么——它可能是一个方法不同的子类。这是因为任何子类都可以在定义基类的地方使用。
在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()的正确版本来匹配参数。
重载是指定义两个名称相同但参数不同的方法
重写是通过子类中具有相同名称的函数更改基类的行为。
所以多态性与覆盖有关,但不是真正的重载。
然而,如果有人对“什么是多态性?”这个问题给出了一个简单的“重写”的答案。我要求进一步解释。
重写和重载都用于实现多态性。
你可以在一个类中有一个方法 它在或中被覆盖 更多的子类。这个方法可以 不同的东西取决于哪个 类用于实例化对象。
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中,所有的类都继承自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.
重载是定义具有相同名称但具有不同参数的多个方法的操作。它与覆盖或多态性无关。