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