1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class Sample
{ private int x; public Sample()
{ //
不帶參數的構造方法 this ( 1 ); } public Sample( int x)
{ //帶參數的構造方法 this .x=x; } public int Sample( int x)
{ //不是構造方法 return x++; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class Mystery
{ private String s; public void Mystery()
{ //不是構造方法 s
= "constructor" ; } void go()
{ System.out.println(s); } public static void main( String []
args) { Mystery
m = new Mystery(); m.go(); } } |
1
2
3
4
5
6
7
8
9
|
public class Sample1{} public class Sample2{ public Sample2( int a){System.out.println( "My
Constructor" );} } public class Sample3{ public Sample3(){System.out.println( "My
Default Constructor" );} } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public class Platypus
{ String name; Platypus( String input)
{ name
= input; } Platypus()
{ this ( "John/Mary
Doe" ); } public static void main( String args[])
{ Platypus
p1 = new Platypus( "digger" ); Platypus
p2 = new Platypus(); System.out.println(p1.name
+ "----" +
p2.name); } } |
需要注意的兩個地方是:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
class getBirthInfo
{ void getBirthInfo()
{ System.out.println( "born
alive." ); } } class Platypus1 extends getBirthInfo { void getBirthInfo()
{ System.out.println( "hatch
from eggs" ); System.out.println( "a
mammal normally is " ); super .getBirthInfo(); } } public class test1
{ public static void main( String []
args) { Platypus1
p1= new Platypus1(); p1.getBirthInfo(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
class getBirthInfo
{ getBirthInfo(){ System.out.println( "auto" ); } void aa()
{ System.out.println( "born
alive." ); } } class Platypus1 extends getBirthInfo { Platypus1()
{ super (); System.out.println( "hatch
from eggs" ); System.out.println( "a
mammal normally is " ); } } public class test1
{ public static void main( String []
args) { Platypus1
p1= new Platypus1(); } } |
類的繼承機制使得子類可以調用父類的功能,下面介紹類在繼承關係的初始化順序問題
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class SuperClass { SuperClass() { System.out.println( "SuperClass
constructor" ); } } public class SubClass extends SuperClass
{ SubClass() { System.out.println( "SubClass
constructor" ); } public static void main( String []
args) { SubClass
sub = new SubClass(); } } |
SubClass constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class SuperClass { SuperClass( String str) { System.out.println( "Super
with a string." ); } } public class SubClass extends SuperClass { SubClass( String str) { System.out.println( "Sub
with a string." ); } public static void main( String []
args) { SubClass
sub = new SubClass( "sub" ); } } |
Sub with a string. 第二種方法即使父類中有顯示的默認構造方法也不會被調用。
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
|
class One { One( String str) { System.out.println(str); } } class Two { One
one_1 = new One( "one-1" ); One
one_2 = new One( "one-2" ); One
one_3 = new One( "one-3" ); Two( String str) { System.out.println(str); } } public class Test { public static void main( String []
args) { System.out.println( "Test
main() start" ); Two
two = new Two( "two" ); } } |
one-1
one-2
one-3
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
|
class One { One( String str) { System.out.println(str); } } class Two { One
one_1 = new One( "one-1" ); One
one_2 = new One( "one-2" ); static One
one_3 = new One( "one-3" ); Two( String str) { System.out.println(str); } } public class Test { public static void main( String []
args) { System.out.println( "Test
main() start" ); Two
two_1 = new Two( "two-1" ); System.out.println( "------------" ); Two
two_2 = new Two( "two-2" ); } } |
one-3
one-1
one-2
two-1
------------
one-1
one-2
two-2
結論:如果一個類中有靜態對象,那麼他會在非靜態對象初始化前進行初始化,但只初始化一次。而非靜態對象每次調用時都要初始化。
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
|
class One { One( String str) { System.out.println(str); } } class Two { One
one_1 = new One( "one-1" ); One
one_2 = new One( "one-2" ); static One
one_3 = new One( "one-3" ); Two( String str) { System.out.println(str); } } public class Test { static Two
two_3 = new Two( "two-3" ); public static void main( String []
args) { System.out.println( "Test
main() start" ); Two
two_1 = new Two( "two-1" ); System.out.println( "------------" ); Two
two_2 = new Two( "two-2" ); } } |
one-1
one-2
two-3
Test main() start
one-1
one-2
two-1
------------
one-1
one-2
two-2
2.主類的父類的構造方法被調用。
3.主類的非靜態對象(變量)初始化。
4.調用主類的構造方法。