一个类初始化问题~谢谢了

yuppy 2009-03-14 11:44:08

public class Test184 extends c184 {
private a184 at = new a184("a in t");
private int i = f();

Test184() {
System.out.println("this is test184");
}

public int f() {
System.out.println("this is t f()");
return 1;
}

public static void main(String[] args) {
Test184 t = new Test184();
}

}

class a184 {
a184(String s) {
System.out.println("this is a.." + s);
}
}

class b184 {
private a184 ab = new a184("a in b");

b184() {
System.out.println("this is b..");
}
}

class c184 extends b184 {
private a184 ac = new a184("a in c");

c184() {
System.out.println("this is c..");
}
}


运行结果:
this is a..a in b
this is b..
this is a..a in c
this is c..
this is a..a in t
this is t f()
this is test184

请问下红色部分为什么不先被初始化呢?
...全文
146 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
CJljfn 2009-03-16
  • 打赏
  • 举报
回复
看看
wangming402407250 2009-03-15
  • 打赏
  • 举报
回复
你先从main()为入口
要实例化Test184需实例化c184,实例化c184需要实例化b184
实例化b184需先要初始化实例变量ab
class b184
{

b184()
{
ab = new a184("a in b");
System.out.println("this is b..");
}

private a184 ab;
}
b184实例化后再实例化c184
同理需要初始化实例变量ac
class c184 extends b184
{

c184()
{
ac = new a184("a in c");
System.out.println("this is c..");
}

private a184 ac;
}
最后实例化Test184初始化实例变量
at,i
public class Test184 extends c184
{

Test184()
{
at = new a184("a in t");
i = f();
System.out.println("this is test184");
}

public int f()
{
System.out.println("this is t f()");
return 1;
}

public static void main(String args[])
{
Test184 test184 = new Test184();
}

private a184 at;
private int i;
}
明白不明白其中的代码是我用反编译工具反编译的.不理解还可以再问
bzwm 2009-03-15
  • 打赏
  • 举报
回复
你这里没有静态变量,
所以类的初始化顺序是 父类的成员变量,然后父类的构造方法,子类的成员变量,子类的构造方法。
你按照我说的这个理顺一下。
popcan 2009-03-15
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 mysky191_chi 的回复:]
初始化大概顺序:

父类静态成员变量
子类静态成员变量
父类非静态成员变量
父类构造函数
子类非静态成员变量 
子类构造函数
[/Quote]

学习...
jesunmy2008 2009-03-15
  • 打赏
  • 举报
回复
main函数是入口,然后依次将调用的那些类递归初始,也就是12楼说的顺序进行初始化,就得出正解了
successgl 2009-03-15
  • 打赏
  • 举报
回复
因为test184是继承于 c184,生成对象时是先要初始化基类的,而c184又继承于b184,所以又要先初始化b184,b184已经是最上层的基类了,所以先初始化b184中的private a184类型的变量ab ,因为变量是a184类型,所以先调用a184的构造函数,this is a..a in b 打印,接着再递归回去!
mysky191_chi 2009-03-15
  • 打赏
  • 举报
回复
初始化大概顺序:

父类静态成员变量
子类静态成员变量
父类非静态成员变量
父类构造函数
子类非静态成员变量 
子类构造函数
LouisHwa 2009-03-15
  • 打赏
  • 举报
回复
初始化子类是,必须先初始化其父类,层层递进


5楼正解,递归啊
老紫竹 2009-03-15
  • 打赏
  • 举报
回复
public class Test184 extends c184 {

系统先递归初始化父类


yuppy 2009-03-15
  • 打赏
  • 举报
回复
好像有点懂了,父类初始化总是要在子类初始化之前被进行的~~
sunxyz86 2009-03-15
  • 打赏
  • 举报
回复
this is a..a in b
this is b..
this is a..a in c
this is c..
this is a..a in t
this is t f()
this is a..a in t..Test184()
this is test184
yuppy 2009-03-15
  • 打赏
  • 举报
回复

public class Test184 extends c184 {
private a184 at = new a184("a in t");
private int i = f();

Test184() {
a184 at1 = new a184("a in t..Test184()");
System.out.println("this is test184");
}

public int f() {
System.out.println("this is t f()");
return 1;
}

public static void main(String[] args) {
Test184 t = new Test184();
}

}

class a184 {
a184(String s) {
System.out.println("this is a.." + s);
}
}

class b184 {
private a184 ab = new a184("a in b");

b184() {
System.out.println("this is b..");
}
}

class c184 extends b184 {
private a184 ac = new a184("a in c");

c184() {
System.out.println("this is c..");
}
}


那我要是在Test184类构造器中再放个变量呢?他是什么时候被初始化?
谢谢大哥们了~~
yuppy 2009-03-15
  • 打赏
  • 举报
回复
在类Test184中的变量at,i 是不是应该在Test184的构造器被初始化之前被先初始化?
yuppy 2009-03-15
  • 打赏
  • 举报
回复
我的意思是Test184类先被初始化,其类中的变量应该是在其构造器之前被初始化吧,那是不是应该先初始化类a184?
是不是我这样理解错的?
yuppy 2009-03-14
  • 打赏
  • 举报
回复
有人不?
yuppy 2009-03-14
  • 打赏
  • 举报
回复
有大哥指导下不?谢谢了~~

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧