类的加载顺序

class Base {
    private String nameBase = "base";

    public Base() {
        System.out.println("nameBase="+nameBase);
        System.out.println("this is Base");
        tellName();
        printName();
    }

    public void tellName() {
        System.out.println("Base tell name: "+nameBase);
    }

    public void printName() {
        System.out.println("Base print name: "+nameBase);
    }
}
public class Dervied extends Base{
    private String name = "derived";

    public Dervied() {
        System.out.println("this is Derived");
        tellName();
        printName();
    }

    public void tellName() {
        System.out.println("Dervied tell name: "+name);
    }
   /* public void printName() {
        System.out.println("Dervied print name:"+name);
    }*/

    public static void main(String[] args) {
        new Dervied();
    }
}


nameBase=base
this is Base
Dervied tell name: null
Base print name: base
this is Derived
Dervied tell name: derived
Base print name: base

class ParentClass {
    public static int a = 2; //必须放到静态代码块前面
    //public int a = 2;
    public int b = 3; // 必须放到匿名代码块的前面,以保证先被初始化后使用
    private String str="ParentClass";

    {
        System.out.println("this is anonymity b="+b+", str = "+str);
    }

    static {
        a = 4;
        System.out.println("this is static and a="+a);
    }

    public void s() {
        System.out.println("this is parent");
    }

    public ParentClass() {
        System.out.println("b="+b);
        System.out.println("this is parent gozao");
        s();
    }
}
public class Sons extends ParentClass{
   public Sons() {
       System.out.println("this is Sons gozao");
    }

    public void s() {
        System.out.println("this is son");
    }

    public static void main(String[] args) {
        ParentClass d = new Sons();
        d.s(); //在父类中通过this.s()调用的是子类的方法,子类的s()方法覆盖了父类的方法后,无论在哪里调用,都是调用子类的方法。
    }
}

/*
    类内的加载顺序为:
        静态变量
        静态代码块
        静态方法

        匿名代码块
        非静态变量

        构造函数
 */


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值