非静态实例的初始化

来源:互联网 发布:淘宝怎么去推广 编辑:程序博客网 时间:2024/05/21 09:14
class Mug{
Mug(int marker){
System.out.println("Cup(" + marker + ")");
}
void fun(int marker){
System.out.println("fun(" + marker + ")");
}
}
class Mugs{
Mug c1;
Mug c2;
{
c1 = new Mug(1);
c2 = new Mug(2);
System.out.println("c1 & c2 initialized");
}
Mugs(){
System.out.println("Mugs()");
}
}
public class Property {
public static void main(String[] args) {
System.out.println("Creating new Mugs in main");
Mugs m = new Mugs();
m.c1.fun(1);
m.c2.fun(2);
}

}


结果:

Creating new Mugs in main
Cup(1)
Cup(2)
c1 & c2 initialized
Mugs()
fun(1)
fun(2)

0 0