這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)java中的Person、Student和GoodStudent類的繼承,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、成都做網(wǎng)站與策劃設(shè)計(jì),豐寧網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:豐寧等地區(qū)。豐寧做網(wǎng)站價格咨詢:028-86922220
有這樣三個類,Person,Student,GoodStudent。其中Student繼承了Person,GoodStudent繼承了Student,三個類中只有默認(rèn)的構(gòu)造函數(shù),用什么樣的方法證明在創(chuàng)建Student類的對象的時候是否調(diào)用了Person的構(gòu)造函數(shù),在創(chuàng)建GoodStudent類的對象的時候是否調(diào)用了Student構(gòu)造函數(shù)?如果在創(chuàng)建Student對象的時候沒有調(diào)用Person的構(gòu)造函數(shù)(我也不知道什么情況下不會去調(diào)用,如果都是默認(rèn)無參構(gòu)造函數(shù)的話),那么采用什么樣的手段可以調(diào)用父類的構(gòu)造函數(shù)?
一、需要分析
1、Person,Student,GoodStudent三個類的繼承關(guān)系
2、實(shí)現(xiàn)三個class的構(gòu)造函數(shù)
3、打印信息查看各個類的構(gòu)造函數(shù)是否被調(diào)用
二、技術(shù)點(diǎn)
1、弄清楚Java 類的無參構(gòu)造函數(shù)是默認(rèn)調(diào)用的
2、如果父類的構(gòu)造函數(shù)是有參的,那么要在子類的構(gòu)造函數(shù)的第一行加入super(args); 來確認(rèn)對哪個父類構(gòu)造函數(shù)的調(diào)用
代碼:
package com.itheima;
/**
* 9、
* 有這樣三個類,Person,Student.GoodStudent。其中Student繼承了Person,GoodStudent繼承了Student,
* 三個類中只有默認(rèn)的構(gòu)造函數(shù),用什么樣的方法證明在創(chuàng)建Student類的對象的時候是否調(diào)用了Person的構(gòu)造函數(shù),
* 在創(chuàng)建GoodStudent類的對象的時候是否調(diào)用了Student構(gòu)造函數(shù)?如果在創(chuàng)建Student對象的時候沒有調(diào)用Person的構(gòu)造函數(shù)
* ,那么采用什么樣的手段可以調(diào)用父類的構(gòu)造函數(shù)?
*
* @author 281167413@qq.com
*/
public class Test9 {
public static void main(String[] args) {
Student s1 = new Student();
System.out.println("-------------------------------");
Student s2 = new Student();
System.out.println("-------------------------------");
GoodStudent g1 = new GoodStudent();
System.out.println("-------------------------------");
}
}
class Person {
Person() {
System.out.println("I'm Person!");
}
Person(String arg) {
System.out.println(arg);
}
Person(String arg1, String arg2) {
System.out.println(arg1 + arg2);
}
}
class Student extends Person {
Student() {
super("have arg!"); //
System.out.println("I'm Student!");
}
Student(String arg) {
super("have arg!", "in Person");
System.out.println(arg);
}
}
class GoodStudent extends Student {
GoodStudent() {
super("from GoodStudent!");
System.out.println("I'm GoodStudent!");
}
}打印構(gòu)造函數(shù)的調(diào)用過程:
have arg! I'm Student! ------------------------------- have arg! I'm Student! ------------------------------- have arg!in Person from GoodStudent! I'm GoodStudent! -------------------------------
上述就是小編為大家分享的java中的Person、Student和GoodStudent類的繼承了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前名稱:java中的Person、Student和GoodStudent類的繼承
地址分享:http://www.chinadenli.net/article22/pgdecc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、Google、、小程序開發(fā)、網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)