当前位置: 代码迷 >> Java相关 >> 求高手!解决办法
  详细解决方案

求高手!解决办法

热度:5854   发布时间:2013-02-25 21:52:18.0
求高手!!!!!!!!
public class Test {
static String a="string-a";
static String b;

String c= "string-c";
String d;

static{
printStatic("before static");
b="string -b";
printStatic ("after static");

}
public static void printStatic(String title){
System.out.println("********"+title+"*********");
System.out.println("a="+a);
System.out.println("b="+b);

}
public Test(){
print("before constructer");
d="string-d";
print("sfter constructer");

}
public void print(String title){
System.out.println("********"+title+"*********");
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
System.out.println("d="+d) ;
}
public static void main (String args[]){
new Test();


}

}





最后一句什么意思??????????


------解决方案--------------------------------------------------------
new Test();
就是调用构造函数嘛,只不过是个匿名对象而已,是为测试用的,对用只用一次。
和普通实例化一个对象一样。
Test test = new Test();
结果一样。
------解决方案--------------------------------------------------------
new Test()
调用Test类得构造方法,即调用
public Test(){
print("before constructer");
d="string-d";
print("sfter constructer");
}



------解决方案--------------------------------------------------------
创建匿名实例对象,就是不用名字引用,只使用一次可以这样创建
  相关解决方案