一个研究java迭代类初始化顺序的程序

该博客为转载内容,转载自https://www.cnblogs.com/Finding2013/archive/2005/09/24/243132.html ,涉及Java相关知识。
None.gifpackage base;
None.gif
abstract class base
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public int age=getNumber(100);
InBlock.gif    
static
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.out.println(
"base static block");
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.out.println(
"base nonstatic block");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
static int sage=getNumber(50);
InBlock.gif    base()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.out.println(age);
InBlock.gif        System.out.println(
"base start");
InBlock.gif        draw();
//会调用子类覆盖后的方法
InBlock.gif
        System.out.println("base end");
InBlock.gif        
ExpandedSubBlockEnd.gif    }

InBlock.gif    
static int getNumber(int base)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.out.println(
"base.getNumber int"+base);
InBlock.gif        
return base;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public  void draw()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.out.println(
"base.draw");
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
public class initializeOrder extends basedot.gif{
InBlock.gif    
public int age=getNumber(1001);
InBlock.gif    
private int _radius=getNumber(10);
InBlock.gif    
static int sage=getNumber(250);
InBlock.gif    
static
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.out.println(
"subclass static block");
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.out.println(
"subclass nonstatic block");
ExpandedSubBlockEnd.gif    }

InBlock.gif    initializeOrder(
int radius)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        _radius
=radius;
InBlock.gif        System.out.println(age);
InBlock.gif        System.out.println(
"initializeOrder initialized");
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public void draw()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.out.println(
"initializeOrder.draw "+_radius);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/** *//**
InBlock.gif     * 
@param args
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        
// TODO Auto-generated method stub
InBlock.gif
        new initializeOrder(1000);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
InBlock.gif    总结:
InBlock.gif    1、类只有在使用New调用创建的时候才会被JAVA类装载器装入
InBlock.gif    2、JAVA类首次装入时,会对静态成员变量或方法进行一次初始化,但方法不被调用是不会执行的
InBlock.gif       静态成员变量和静态初始化块级别相同,非静态成员变量和非静态初始化块级别相同
InBlock.gif       先初始化父类的静态代码--->初始化子类的静态代码-->
InBlock.gif       初始化父类的非静态代码--->初始化父类构造函数--->
InBlock.gif       初始化子类非静态代码--->初始化子类构造函数
InBlock.gif    3、创建类实例时,首先按照父子继承关系进行初始化
InBlock.gif    4、类实例创建时候,首先初始化块部分先执行,然后是构造方法;然后从
InBlock.gif       本类继承的子类的初始化块执行,最后是子类的构造方法
InBlock.gif    5、类消除时候,首先消除子类部分,再消除父类部分
ExpandedSubBlockEnd.gif    
*/

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/Finding2013/archive/2005/09/24/243132.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值