多态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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mark_to_win

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值