java學(xué)習(xí)-構(gòu)造函數(shù)發(fā)布者:本站 時(shí)間:2020-05-06 15:05:11
一直學(xué)習(xí)java,都沒有把構(gòu)造函數(shù)弄清楚,通過昨天的學(xué)習(xí),現(xiàn)在有了一點(diǎn)點(diǎn)理解,不知道還有什么不知道的,望大家指導(dǎo)指導(dǎo)。
當(dāng)父類沒有任何構(gòu)造函數(shù),子類定義了一個(gè)不帶參數(shù)的構(gòu)造函數(shù),這時(shí)候在測(cè)試類里面創(chuàng)建一個(gè)子類的對(duì)象時(shí),程序運(yùn)行時(shí),首先調(diào)用系統(tǒng)默認(rèn)的父類的構(gòu)造函數(shù),然后再調(diào)用子類的構(gòu)造函數(shù)。例子:
public class SuperClass {
private String name;
private String birthdate;
public void show() {
System.out.println("this is a public function" + name + birthdate);
}
}
public class SubClass extends SuperClass {
String address;
public SubClass() {
System.out.println("SubClass:this is a no parameter constructor");
}
public void personinformation(String _name, String _birth, String _address) {
System.out.println("output information" + _name + _birth + _address);
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SubClass absence = new SubClass();
}
}
運(yùn)行結(jié)果:SubClass:this is a no parameter constructor
當(dāng)父類里面定義了幾個(gè)構(gòu)造函數(shù),其中至少有一個(gè)不帶參數(shù),子類定義了一個(gè)不帶參數(shù)的構(gòu)造函數(shù),運(yùn)行的時(shí)候,調(diào)用不帶參數(shù)的構(gòu)造函數(shù),然后再運(yùn)行子類的構(gòu)造函數(shù)。
public class SuperClass {
private String name;
private String birthdate;
public SuperClass() {
name = "chen";
birthdate = "feb";
System.out.println("SuperClass:this is a no parameter constructor");
}
public SuperClass(String chen, String febu) {
name = chen;
birthdate = febu;
System.out.println("this is parameter constructor:" + name + birthdate);
}
public void show() {
System.out.println("this is a public function" + name + birthdate);
}
}
public class SubClass extends SuperClass {
String address;
public SubClass() {
System.out.println("SubClass:this is a no parameter constructor");
}
public void personinformation(String _name, String _birth, String _address) {
System.out.println("output information" + _name + _birth + _address);
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SubClass absence = new SubClass();
}
}
運(yùn)行結(jié)果:SuperClass:this is a no parameter constructor
SubClass:this is a no parameter constructor
當(dāng)父類中只定義帶參數(shù)的構(gòu)造函數(shù),父類定義的構(gòu)造函數(shù)和上面的一樣,這樣程序運(yùn)行就會(huì)出現(xiàn)錯(cuò)誤,編譯不能通過,會(huì)出現(xiàn)錯(cuò)誤提示。因?yàn)樵趧?chuàng)建子類對(duì)象時(shí),由于沒有不帶參數(shù)的構(gòu)造函數(shù),它不知道要調(diào)用父類的那個(gè)構(gòu)造函數(shù)。所以要在定義子類構(gòu)造的第一行加上調(diào)用父類的構(gòu)造函數(shù):super("hhh");根據(jù)括號(hào)里面的參數(shù)類型、個(gè)數(shù)來確定調(diào)用父類里面的那個(gè)構(gòu)造函數(shù)。例子:
public class SuperClass {
private String name;
private String birthdate;
public SuperClass(String chen, String febu) {
name = chen;
birthdate = febu;
System.out.println("this is parameter constructor:" + name + birthdate);
}
public SuperClass(String chen) {
name = chen;
System.out.println("this is parameter constructor1:" + name );
}
public void show() {
System.out.println("this is a public function" + name + birthdate);
}
}
public class SubClass extends SuperClass {
String address;
public SubClass() {
super("hhh"); //非常重要!根據(jù)他來調(diào)用父類的構(gòu)造函數(shù)
System.out.println("SubClass:this is a no parameter constructor");
}
public void personinformation(String _name, String _birth, String _address) {
System.out.println("output information" + _name + _birth + _address);
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SubClass absence = new SubClass();
}
}
運(yùn)行結(jié)果:this is parameter constructor1:hhh
選擇我們,優(yōu)質(zhì)服務(wù),不容錯(cuò)過
1. 優(yōu)秀的網(wǎng)絡(luò)資源,強(qiáng)大的網(wǎng)站優(yōu)化技術(shù),穩(wěn)定的網(wǎng)站和速度保證
2. 15年上海網(wǎng)站建設(shè)經(jīng)驗(yàn),優(yōu)秀的技術(shù)和設(shè)計(jì)水平,更放心
3. 全程省心服務(wù),不必?fù)?dān)心自己不懂網(wǎng)絡(luò),更省心。
------------------------------------------------------------
24小時(shí)聯(lián)系電話:021-58370032