class init & object creation

[b]class init & object creation in inheritance[/b]

example:

execute SelfBall.java 's main() function,then get the result.

[b]GrandpaBall.java[/b]

/**
* grandpa ball
* @author eric
* @date 2010-3-19 上午11:43:42
*/
public class GrandpaBall {
// static fields
protected static int gx = gxInit();
protected static int gy;
// non-static fields
protected int ga = gaInit();
// static block
static {
System.out.println("grandpa static block init.");
}
// non-static block
{
System.out.println("grandpa non-static block init.");
}

// constructor
public GrandpaBall() {
System.out.println("grandpa constructor execute.");
}

private static int gxInit() {
System.out.println("grandpa static field init.");
return 1;
}

private int gaInit() {
System.out.println("grandpa non-static field init.");
return 1;
}
}


[b]FatherBall.java[/b]

/**
* father ball
* @author eric
* @date 2010-3-19 上午11:44:01
*/
public class FatherBall extends GrandpaBall {
// static fields
protected static int fx = fxInit();
protected static int fy;
// non-static fields
protected int fa = faInit();
// static block
static {
System.out.println("father static block init.");
}
// non-static block
{
System.out.println("father non-static block init.");
}

// constructor
public FatherBall() {
System.out.println("father constructor execute.");
}

private static int fxInit() {
System.out.println("father static field init.");
return 2;
}

private int faInit() {
System.out.println("father non-static field init.");
return 2;
}
}


[b]SelfBall.java[/b]

/**
* self ball,test class init & object creation
* @author eric
* @date 2010-3-19 上午11:44:19
*/
public class SelfBall extends FatherBall {
// static fields
protected static int sx = sxInit();
protected static int sy;
// non-static fields
protected int sa = saInit();
// static block
static {
System.out.println("self static block init.");
}
// non-static block
{
System.out.println("self non-static block init.");
}

// constructor
public SelfBall() {
System.out.println("self constructor execute.");
}

private static int sxInit() {
System.out.println("self static field init.");
return 3;
}

private int saInit() {
System.out.println("self non-static field init.");
return 3;
}

// main
public static void main(String[] args) {
System.out.println();
System.out.println("executeing main() ... class has been loaded.");
System.out.println("object is going to be created:");
System.out.println();
// create object
SelfBall sb1 = new SelfBall();
System.out.println();
System.out.println("object created.");
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯智能台灯

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值