什么是多态性,它的用途是什么,如何使用它?
当前回答
多态性是一个对象具有多种形式的能力。在OOP中,多态性最常见的用法是用父类引用引用子类对象。在这个用Java编写的例子中,我们有三种类型的车辆。我们创建了三个不同的对象,并尝试运行他们的轮子方法:
public class PolymorphismExample {
public static abstract class Vehicle
{
public int wheels(){
return 0;
}
}
public static class Bike extends Vehicle
{
@Override
public int wheels()
{
return 2;
}
}
public static class Car extends Vehicle
{
@Override
public int wheels()
{
return 4;
}
}
public static class Truck extends Vehicle
{
@Override
public int wheels()
{
return 18;
}
}
public static void main(String[] args)
{
Vehicle bike = new Bike();
Vehicle car = new Car();
Vehicle truck = new Truck();
System.out.println("Bike has "+bike.wheels()+" wheels");
System.out.println("Car has "+car.wheels()+" wheels");
System.out.println("Truck has "+truck.wheels()+" wheels");
}
}
结果是:
欲了解更多信息,请访问https://github.com/m-vahidalizadeh/java_advanced/blob/master/src/files/PolymorphismExample.java。我希望这能有所帮助。
其他回答
一般来说,它是一种使用相同或表面上相似的API来为许多不同类型的对象提供接口的能力。有多种形式:
Function overloading: defining multiple functions with the same name and different parameter types, such as sqrt(float), sqrt(double) and sqrt(complex). In most languages that allow this, the compiler will automatically select the correct one for the type of argument being passed into it, thus this is compile-time polymorphism. Virtual methods in OOP: a method of a class can have various implementations tailored to the specifics of its subclasses; each of these is said to override the implementation given in the base class. Given an object that may be of the base class or any of its subclasses, the correct implementation is selected on the fly, thus this is run-time polymorphism. Templates: a feature of some OO languages whereby a function, class, etc. can be parameterised by a type. For example, you can define a generic "list" template class, and then instantiate it as "list of integers", "list of strings", maybe even "list of lists of strings" or the like. Generally, you write the code once for a data structure of arbitrary element type, and the compiler generates versions of it for the various element types.
如果你想想这个词的希腊词根,它就会变得很明显。
Poly = many: polygon = many-sided, polystyrene = many苯乙烯(a), polyglot =多种语言,等等。 Morph =变化或形式:形态学=对生物形态的研究,Morpheus =希腊梦之神,可以变成任何形式。
因此,多态性是(在编程中)为不同的底层形式(数据类型)提供相同接口的能力。
例如,在许多语言中,整数和浮点数都是隐式多态的,因为你可以加、减、乘等等,而不管它们的类型是否不同。在通常的术语中,它们很少被视为对象。
但是,以同样的方式,像BigDecimal、Rational或Imaginary这样的类也可以提供这些操作,尽管它们操作的数据类型不同。
典型的例子是Shape类和所有可以从它继承的类(正方形、圆形、十二面体、不规则多边形、splat等等)。
对于多态性,每个类都有不同的底层数据。一个点形状只需要两个坐标(当然假设它是在二维空间中)。圆需要圆心和半径。一个正方形或矩形的左上角和右下角需要两个坐标,(可能)还需要一个旋转。一个不规则的多边形需要一系列的线。
通过使类对其代码和数据负责,您可以实现多态性。在这个例子中,每个类都有自己的Draw()函数,客户端代码可以简单地这样做:
shape.Draw()
为了得到任何形状的正确行为。
这与旧的处理方法相反,旧的方法中代码与数据是分开的,并且您将拥有像drawSquare()和drawCircle()这样的函数。
面向对象、多态和继承都是密切相关的概念,了解它们至关重要。在我漫长的职业生涯中,有许多“银弹”基本上都失败了,但OO范例已经被证明是一个很好的范例。学习它,理解它,爱上它——你会为你所做的感到高兴:)
(a)我最初写这个是作为一个笑话,但它被证明是正确的,因此,并不那么有趣。单体苯乙烯是由碳和氢组成的,C8H8,聚苯乙烯是由(C8H8)n组成的。
也许我应该说明,息肉是字母p的多次出现,尽管现在我必须解释这个笑话,即使这似乎也不好笑。
有时候,你应该在落后的时候就放弃:-)
在面向对象语言中,多态性允许通过同一个接口处理和处理不同的数据类型。例如,考虑c++中的继承: 类B派生自类A。类型为A*的指针(指向类A的指针)可以用来处理类A的对象和类B的对象。
多态性是一个对象具有多种形式的能力。在OOP中,多态性最常见的用法是用父类引用引用子类对象。在这个用Java编写的例子中,我们有三种类型的车辆。我们创建了三个不同的对象,并尝试运行他们的轮子方法:
public class PolymorphismExample {
public static abstract class Vehicle
{
public int wheels(){
return 0;
}
}
public static class Bike extends Vehicle
{
@Override
public int wheels()
{
return 2;
}
}
public static class Car extends Vehicle
{
@Override
public int wheels()
{
return 4;
}
}
public static class Truck extends Vehicle
{
@Override
public int wheels()
{
return 18;
}
}
public static void main(String[] args)
{
Vehicle bike = new Bike();
Vehicle car = new Car();
Vehicle truck = new Truck();
System.out.println("Bike has "+bike.wheels()+" wheels");
System.out.println("Car has "+car.wheels()+" wheels");
System.out.println("Truck has "+truck.wheels()+" wheels");
}
}
结果是:
欲了解更多信息,请访问https://github.com/m-vahidalizadeh/java_advanced/blob/master/src/files/PolymorphismExample.java。我希望这能有所帮助。
在面向对象编程中,多态性指的是编程语言根据对象的数据类型或类对对象进行不同处理的能力。更具体地说,它是为派生类重新定义方法的能力。