多态性(源自希腊语,意为“多种形态”)允许将不同类的对象视为同一超类的对象。它支持单一接口,例如…… 方法 名称,用于指代一类通用的操作。具体操作由运行时对象的确切类型决定。这通常通过方法重写来实现。

(图片仅供参考)
多态性(源自希腊语,意为“多种形态”)允许将不同类的对象视为同一超类的对象。它支持单一接口,例如…… 方法 名称,用于指代一类通用的操作。具体操作由运行时对象的确切类型决定。这通常通过方法重写来实现。
Polymorphism is a powerful concept that allows for writing flexible and decoupled code. The most common form in OOP is subtype polymorphism, which is enabled by inheritance and method overriding. When a subclass provides a specific implementation for a method that is already defined in its superclass, it is called overriding. A variable of the superclass type can refer to an object of any of its subclasses. When a method is called on this variable, the version of the method that gets executed is the one belonging to the object’s actual class, not the variable’s type. This decision is made at runtime, a process known as dynamic dispatch or late binding.
例如,考虑一个名为“Shape”的超类,它有一个方法“calculateArea()”。像“Circle”和“Rectangle”这样的子类可以继承自“Shape”,并提供它们各自特定的“calculateArea()”实现。程序可以拥有一个“Shape”对象列表,其中包含“Circle”和“Rectangle”实例。当遍历该列表并对每个元素调用“calculateArea()”时,系统会自动为每个形状使用正确的公式。这样就无需使用冗长的“if-else”或“switch”语句来检查每个对象的类型,从而编写出更简洁、更易于扩展的代码。无需修改处理新形状的代码,即可向系统中添加新的形状。
多态性(编程)
(如果日期未知或不相关,例如“流体力学”,则提供其显著出现的近似估计)
只有注册会员才能免费获得 100% 的全尺寸图片和下载。.
> 登录 <