多态

package com.yudianxx.basic.duotai;

public class Chongzai {

    public static void main(String[] args) {
//        A a = new A();
//        B b = new B();
//        C c = new C();
//        A a = new B();
//        a.same();
//        a.a();
//        a = (B) a;
//        a.same();
//        ((B) a).same();
//        ((B) a).b();
//        A ab = new B(1);  //看new B() 的构造函数
//        B bc = new C();
//        C c = new C();
//        A a = new A();
//        a.same();
//        a = (B) a; //错误 大概等价于 C a = new A() ?
        B b =new C();
        b.same();
        b =(C) b; //等价于 C cc = (C) b; cc与b一样
//        cc.same();

//        b = (C) b;
//        b.same();


//        c.b();
//        c.a();

    }
}

class A {
    public A(int i) {
        System.out.println("construct A:" + i);
        same();
    }

    public A() {
        System.out.println("construct A");
        same();
    }

    public void same() {
        System.out.println("A");
    }

    public void a() {
        System.out.println("a");
    }
}

class B extends A {
    public B() {
//        super();  //默认调用父类的无参构造方法,可写可不写,如果调用有参的则不会默认调用无参的
//        super(1);
        System.out.println("construct B");
        same();
    }

    public B(int i) {
        //默认调用父类的无参构造方法,可写可不写,如果调用有参的则不会默认调用无参的
        System.out.printf(i + "");
    }

    public void same() {
        System.out.println("B");
    }

    public void b() {
        System.out.println("b");
    }
}

class C extends B {
    public C() {
        System.out.println("construct C");
        same();
    }

    public void same() {
        System.out.println("C");
    }

    public void c() {
        System.out.println("c");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

醋酸菌HaC

请我喝杯奶茶吧

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

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

打赏作者

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

抵扣说明:

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

余额充值