-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLesson4_10.java
72 lines (63 loc) · 1.71 KB
/
Lesson4_10.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
static 關鍵字
1.靜態變量或方法不屬於對象,依賴類
2.靜態變量是全局變量,生命週期從類被加載後一值到程序結束
3.靜態變量只有一份,在靜態方法區中儲存
4.靜態變量是本類所有對象共享一份
5.建議不要用對象名去調用靜態數據,直接使用類名調用
6.static 修飾一個方法,那麼該方法屬於類,不屬於對象,直接用類名調用
7.靜態方法不能訪問非靜態的屬性和方法,只能訪問靜態
*/
public class Lesson4_10{
public static void main(String[] args){
/* Role brr = new Role("倍倍","屬國");
Role yun = new Role("云云","屬國");
Role fei = new Role("飛飛","屬國"); */
System.out.println(args.length);
for(String x:args){
System.out.println(x);
}
Role brr = new Role("倍倍");
Role yun = new Role("云云");
Role fei = new Role("飛飛");
System.out.println(brr.getInfo());
System.out.println(yun.getInfo());
System.out.println(fei.getInfo());
System.out.println(brr.country);
System.out.println(yun.country);
System.out.println(fei.country);
brr.country="晉國";
System.out.println("---");
System.out.println(brr.country);
System.out.println(yun.country);
System.out.println(fei.country);
System.out.println("---");
System.out.println(Role.country);//用類名調用靜態變量
}
}
class Role{
private String name;
static String country = "屬國";//靜態變量(全局變量)
public Role(String name){
this.name=name;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
//靜態方法不能訪問非靜態的數據
public static void setCountry(String country){
Role.country=country;
}
/* public void setCountry(String country){
this.country = country;
}
public String getCountry(){
return country;
} */
public String getInfo(){
return "Name"+name+"國家"+country;
}
}