多态polymorphism,向上转型和动态方法调度有什么用?
多态有什么用?马 克 - t o - w i n:https://blog.csdn.net/qq_44639795/article/details/103117332我给大家想了两个需求: 1)要求程序运行起来以后,如果用户输入自行车,就执行自行车的驾驶方法。如果用户输入小轿车,就执行小轿车的驾驶方法。这是就用到父类指针指向子类时的override。2)如果你有一千个子类。要求你依次执行这一千个子类当中的打印。你当然可以一个一个实例化子类后分别执行。马克-to-win:累也累死了,你可以编一个循环。用通用的基类指向所有的派生类。几行程序即可,你可以参照本节的例子。不用这技术, 还真解决不了这问题!
Polymorphism means one type,many form
Dynamic method binding(dynamic method dispatch),方法覆盖仅在两个方法的名称和类型声明都相同时才发生(override)。
动态方法调度(dynamic method dispatch)是一种在运行时而不是编译时调用方法的机制。
动态方法调度也是Java实现运行时多态性的基础。 马克-to-win:要想实现多态,父类和子类必须同时拥有这个同名函数。否则实现不了多态, 底下给出了例子,说明这点。note that when
1)base pointer point to derived class to realize dynamic dispatching,an important requirement is that you also need to
have the same-name method in the base class.refer to the following example of SuperClass.
抽象类和非抽象类二者都可以用来创建对象引用,马克-to-win:用来指向一个子类对象,实现多态。note that abstract and non-abstract class both can dynamically bind for example the following example.
例1.8.1---
abstract class FigureMark {
double dime1;
double dime2;
/*这里的构造函数,是为子类调用使的,不是用来实例化的。马克-to-win: constructor is for subclass's constructor's calling, not for
instantiating. */
FigureMark(double a, double b) {
dime1 = a;
dime2 = b;
}
// area is now an abstract method
abstract void area();
}
class RectangleMark extends FigureMark {
RectangleMark(double a, double b) {
super(a, b);
}
// 覆盖
void area() {
System.out.println("四边形" + dime1 * dime2);
}
}
class TriangleMark extends FigureMark {
TriangleMark(double a, double b) {
super(a, b);
}
void area() {
System.out.println("三角形." + dime1 * dime2 / 2);
}
}
public class Test {
public static void main(String args[]) {
// FigureMark f = new FigureMark(10, 10); // 错,illegal now
RectangleMark r = new RectangleMark(9, 5);
TriangleMark t = new TriangleMark(10, 8);
FigureMark figref; // 没事,this is OK, no object is created
figref = r;
figref.area();
figref = t;
figref.area();
/* 基类指针指向派生类的最大意义就在于此, */
FigureMark[] aa = { r, t };
for (int i = 0; i < aa.length; i++) {
aa[i].area();
}
/* 基类指针指向派生类的最大意义就在于此,马克-to-win:否则你能像下面这样自动话吗?计算机的最大意义不就是自动化, 提高效率吗? */
FigureMark[] aa1 = { new RectangleMark(9, 5), new TriangleMark(10, 8) };
for (int i = 0; i < aa1.length; i++) {
aa1[i].area();
}
}
}
更多内容请见原文,原文转载自:https://blog.csdn.net/qq_44639795/article/details/103117332