創(chuàng)建類時(shí)指定了有參數(shù)構(gòu)造函數(shù)后,系統(tǒng)默認(rèn)不會(huì)創(chuàng)建無(wú)參數(shù)構(gòu)造函數(shù),需要自己手動(dòng)創(chuàng)建。
創(chuàng)建子類的對(duì)象實(shí)例時(shí),默認(rèn)會(huì)先調(diào)用父類的無(wú)參數(shù)的構(gòu)造函數(shù)(默認(rèn)構(gòu)造函數(shù))。
若父類未定義無(wú)參數(shù)構(gòu)造函數(shù),則在編譯階段報(bào)錯(cuò)。
若子類指定了父類的有參構(gòu)造函數(shù),則可以通過(guò)編譯和運(yùn)行。
子類聲明super(id, city)顯示調(diào)用父類有參構(gòu)造函數(shù)
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
|
package cn.lw.testpkg; /** * @author wanglei 2018年4月18日 */ class predessor { private int id; private string city; public predessor( int id, string city) { this .id = id; this .city = city; } @override public string tostring() { return "predessor [id=" + id + ", city=" + city + "]" ; } } class successor extends predessor { private string name; private string sex; public successor(string name, string sex, int id, string city) { super (id, city); this .name = name; this .sex = sex; } @override public string tostring() { return "successor [name=" + name + ", sex=" + sex + "]" ; } } public class callconstructortest { public static void main(string[] args) { successor s2 = new successor( "a" , "male" , 1 , "hz" ); system.out.println(s2); } } |
輸出
successor [name=a, sex=male]
父類未定義無(wú)參數(shù)構(gòu)造函數(shù),子類不聲明super(id, city)
1
2
3
4
|
public successor(string name, string sex) { this .name = name; this .sex = sex; } |
編譯報(bào)錯(cuò)
implicit super constructor predessor() is undefined. must explicitly invoke another constructor
父類顯示定義無(wú)參數(shù)構(gòu)造函數(shù),方法調(diào)用的傳遞性
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
|
package cn.lw.testpkg; /** * @author wanglei 2018年4月18日 */ class predessor { private int id; private string city; public predessor() { system.out.println( "調(diào)用了 predessor 無(wú)參的構(gòu)造函數(shù)" ); } public predessor( int id, string city) { this .id = id; this .city = city; } @override public string tostring() { return "predessor [id=" + id + ", city=" + city + "]" ; } } class successor extends predessor { private string name; private string sex; public successor() { system.out.println( "調(diào)用了 successor 無(wú)參的構(gòu)造函數(shù)" ); } public successor(string name, string sex) { this .name = name; this .sex = sex; } @override public string tostring() { return "successor [name=" + name + ", sex=" + sex + "]" ; } } public class callconstructortest { public static void main(string[] args) { successor s = new successor(); system.out.println(s); system.out.println( "--------------" ); successor s2 = new successor( "a" , "male" ); system.out.println(s2); } } |
輸出
調(diào)用了 predessor 無(wú)參的構(gòu)造函數(shù)
調(diào)用了 successor 無(wú)參的構(gòu)造函數(shù)
successor [name=null, sex=null]
--------------
調(diào)用了 predessor 無(wú)參的構(gòu)造函數(shù)
successor [name=a, sex=male]
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000014449811