File tree Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Expand file tree Collapse file tree 1 file changed +11
-1
lines changed Original file line number Diff line number Diff line change 3
3
public class ConstructorTest {
4
4
public static void main (String [] args ) {
5
5
Emplogee [] staff = new Emplogee [3 ];
6
+
7
+ /* 这里利用到了Java中的“重载”进行初始化,
8
+ * 当然在这里就不能像C++中叫“函数重载”了,
9
+ * 在Java中,应该叫做“方法重载”
10
+ */
6
11
staff [0 ] = new Emplogee ("Harry" , 40000 );
7
12
staff [1 ] = new Emplogee (60000 );
8
13
staff [2 ] = new Emplogee ();
@@ -24,16 +29,22 @@ class Emplogee {
24
29
private String name = "" ;
25
30
private double salary ;
26
31
32
+ /* 下面的代码块用到的是“静态初始化块”
33
+ * 当对类的静态域进行初始化的代码比较复杂的时候,
34
+ * 可以考虑使用“静态初始化块”进行初始化
35
+ */
27
36
static {
28
37
Random generator = new Random ();
29
38
nextId = generator .nextInt (10000 );
30
39
}
31
40
41
+ /* 这里用到的是普通的初始化块 */
32
42
{
33
43
id = nextId ;
34
44
nextId ++;
35
45
}
36
46
47
+ /* 下面三个构造器代码块体现了所谓的“方法重载” */
37
48
public Emplogee (String n , double s ) {
38
49
name = n ;
39
50
salary = s ;
@@ -58,4 +69,3 @@ public int getId() {
58
69
return id ;
59
70
}
60
71
}
61
-
You can’t perform that action at this time.
0 commit comments