> Polymorphism means that a variable of a supertype can refer to a subtype object.
> Polymorphism means that a variable of a supertype can refer to a subtype object.
>
>
> 多态是指直接或者间接的父类型引用变量可以用来引用字类型的对象。很难理解,有什么用?
> 多态是指直接或者间接的父类型引用变量可以用来引用子类型的对象。很难理解,有什么用?
>
>
> A class defines a type. A type defined by a subclass is called a subtype, and a type defined by its superclass is called a supertype. Therefore, you can say that Circle is a subtype of GeometricObject and GeometricObject is a supertype for Circle.
> A class defines a type. A type defined by a subclass is called a subtype, and a type defined by its superclass is called a supertype. Therefore, you can say that Circle is a subtype of GeometricObject and GeometricObject is a supertype for Circle.
但是为什么输入结果不是`This is fruit`?这时因为子类型覆盖了父类型的`toString()`方法;真正的行为表现形式还要看对象的确切类型是什么。例子中,对象是 Apple 或者是 Orange;toString()的行为也只能是Apple或者Orange对应这个函数的真是行为,而不是他父类的行为(父类的这个行为被覆盖了)。关于这个问题,在下面的动态函数绑定中还有解释。
但是为什么输入结果不是`This is fruit`?这时因为子类型覆盖了父类型的`toString()`方法;真正的行为表现形式还要看对象的确切类型是什么。例子中,对象是 Apple 或者是 Orange;toString()的行为也只能是Apple或者Orange对应这个函数的真是行为,而不是他父类的行为(父类的这个行为被覆盖了)。关于这个问题,在下面的动态函数绑定中还有解释。