-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticParameter.java
More file actions
80 lines (61 loc) · 1.65 KB
/
StaticParameter.java
File metadata and controls
80 lines (61 loc) · 1.65 KB
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
73
74
75
76
77
78
79
80
package class_ex;
/**
* static 關鍵字
* - public static => 可視為全局變數、全局方法
* - 使用 static parameter, method 時候,不可以使用 this 關鍵字
* - 不需要實體化即可直接存取,非靜態成員需要先被new建立一個實體 (物件) 才可以訪問
*/
public class StaticParameter {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
s1.name = "George";
s1.age = 20;
s2.name = "Mary";
s2.age = 33;
s1.output();
s2.output();
KKStudent k1 = new KKStudent();
KKStudent k2 = new KKStudent();
k1.name = "kk1";
k2.name = "kk2";
KKStudent.age = 24; // k1.age = 24; // bad style.
k1.output();
k2.output();
}
}
class Student {
final static int max_age = 120;
public int age;
public String name;
public void output() {
System.out.println("Name: " + name + "; Age: " + age);
}
boolean is_outOfMaxage() {
//! 使用 static parameter, method 時候,不可以使用 this 關鍵字
if (this.age > max_age) {
return true;
}
return false;
}
}
class KKStudent {
public static int age; //! 跟Student差別在於 static
public String name;
KKStudent() {
}
KKStudent(String name) {
this.name = name;
}
public void output() {
System.out.println("Name: " + name + "; Age: " + age);
}
}
class Team {
public static String team;
public static String name;
static {
team = "GoGo team";
name = "EX-man";
}
}