看不懂???
public class HelloWorld {
? ??
? ? String name; // 聲明變量name
String sex; // 聲明變量sex
static int age;// 聲明靜態變量age
? ??
? ? // 構造方法
public name() {?
System.out.println("通過構造方法初始化name");
name = "tom";
}
? ??
? ? // 初始化塊
{?
System.out.println("通過初始化塊初始化sex");
sex = "男";
}
? ??
? ? // 靜態初始化塊
static {?
System.out.printlnst("通過靜態初始化塊初始化age");
age = 20;
}
? ??
public void show() {
System.out.println("姓名:" + name + ",性別:" + sex + ",年齡:" + age);
}
? ??
public static void main(String[] args) {
? ? ? ??
? ? ? ? // 創建對象
HelloWorld hello = new HelloWorld();
// 調用對象的show方法
? ? ? ? HelloWorld.show();
? ? ? ??
}
}
2018-06-17
錯誤①:構造方法名應與類名相同
②:調用對象show方法應為:hello.show();