-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticTest.java
More file actions
80 lines (61 loc) · 1.47 KB
/
Copy pathStaticTest.java
File metadata and controls
80 lines (61 loc) · 1.47 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 com.ch04.StaticTest;
public class StaticTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee[] staff = new Employee[3];
staff[0] = new Employee("Tom", 40000);
staff[0].setId();
staff[1] = new Employee("Dick", 60000);
staff[1].setId();
staff[2] = new Employee("Harry", 60000);
staff[2].setId();
//print out information about all Employee Objects
for(Employee e : staff) {
e.getId();
System.out.println("name= " + e.getName() + ",id= "
+ e.getId() + ",salary=" + e.getSalary());
}
int n = Employee.getNextId(); //call static method
System.out.println("Next available id=" + n);
}
}
class Employee{
private static int nextId = 1;
private String name;
private double salary;
private int id;
public Employee(String n, double s) {
name = n;
salary = s;
id = 0;
}
public static int getNextId() {
return nextId;
}
public static void setNextId(int nextId) {
Employee.nextId = nextId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public int getId() {
return id;
}
public void setId() {
id = nextId;
nextId++; //ÓÃÓÚ¼ÆÊýÔ±¹¤Êý
}
public static void main(String[] args) { //unit test
Employee e = new Employee("harry", 50000);
System.out.println(e.getName() + " " + e.getSalary());
}
}