Skip to content

Commit 2f01b3a

Browse files
committed
静态方法
1 parent 067496c commit 2f01b3a

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Part3/StaticTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
public class StaticTest {
2+
public static void main(String[] args) {
3+
Employee[] staff = new Employee[3];
4+
5+
staff[0] = new Employee("Tom", 40000);
6+
staff[1] = new Employee("Dick", 60000);
7+
staff[2] = new Employee("Harry", 65000);
8+
9+
for (Employee e : staff) {
10+
e.setId();
11+
System.out.println(
12+
"name = " + e.getName() +
13+
", id = " + e.getID() +
14+
", salary = " + e.getSalary()
15+
);
16+
}
17+
int n = Employee.getNextId();
18+
System.out.println("Next available id = " + n);
19+
}
20+
}
21+
22+
class Employee {
23+
private static int nextId = 1;
24+
25+
26+
private String name;
27+
private double salary;
28+
private int id;
29+
30+
public Employee(String n, double s) {
31+
name = n;
32+
salary = s;
33+
id = 0;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public double getSalary() {
41+
return salary;
42+
}
43+
44+
public int getID() {
45+
return id;
46+
}
47+
48+
public void setId() {
49+
id = nextId;
50+
nextId++;
51+
}
52+
53+
public static int getNextId() {
54+
return nextId;
55+
}
56+
57+
public static void main(String[] args) {
58+
Employee e = new Employee("Harry", 50000);
59+
System.out.println(e.getName() + " " + e.getSalary());
60+
}
61+
}

0 commit comments

Comments
 (0)