java Person,Student,GoodStudent 三個(gè)類的繼承、構(gòu)造函數(shù)的執(zhí)行
有這樣三個(gè)類,Person,Student,GoodStudent。其中Student繼承了Person,GoodStudent繼承了Student,三個(gè)類中只有默認(rèn)的構(gòu)造函數(shù),用什么樣的方法證明在創(chuàng)建Student類的對象的時(shí)候是否調(diào)用了Person的構(gòu)造函數(shù),在創(chuàng)建GoodStudent類的對象的時(shí)候是否調(diào)用了Student構(gòu)造函數(shù)?如果在創(chuàng)建Student對象的時(shí)候沒有調(diào)用Person的構(gòu)造函數(shù)(我也不知道什么情況下不會(huì)去調(diào)用,如果都是默認(rèn)無參構(gòu)造函數(shù)的話),那么采用什么樣的手段可以調(diào)用父類的構(gòu)造函數(shù)?
一、需要分析
1、Person,Student,GoodStudent三個(gè)類的繼承關(guān)系
2、實(shí)現(xiàn)三個(gè)class的構(gòu)造函數(shù)
3、打印信息查看各個(gè)類的構(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è)父類構(gòu)造函數(shù)的調(diào)用
代碼:
package com.itheima;
/**
* 9、
* 有這樣三個(gè)類,Person,Student.GoodStudent。其中Student繼承了Person,GoodStudent繼承了Student,
* 三個(gè)類中只有默認(rèn)的構(gòu)造函數(shù),用什么樣的方法證明在創(chuàng)建Student類的對象的時(shí)候是否調(diào)用了Person的構(gòu)造函數(shù),
* 在創(chuàng)建GoodStudent類的對象的時(shí)候是否調(diào)用了Student構(gòu)造函數(shù)?如果在創(chuàng)建Student對象的時(shí)候沒有調(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! -------------------------------
相關(guān)文章
SPRING BOOT啟動(dòng)命令參數(shù)及源碼詳析
這篇文章主要給大家介紹了關(guān)于SPRING BOOT啟動(dòng)命令參數(shù)及源碼分析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用SPRING BOOT具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
String StringBuilder StringBuffer區(qū)別以及源碼分析
string是C++標(biāo)準(zhǔn)庫的一個(gè)重要的部分,主要用于字符串處理。可以使用輸入輸出流方式直接進(jìn)行string操作,同時(shí),C++的算法庫對string類也有著很好的支持,并且string類還和c語言的字符串之間有著良好的接口2021-06-06
Spring Boot應(yīng)用發(fā)布到Docker的實(shí)現(xiàn)
這篇文章主要介紹了Spring Boot應(yīng)用發(fā)布到Docker的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
深入了解Java中循環(huán)結(jié)構(gòu)的使用
Java中有三種主要的循環(huán)結(jié)構(gòu):while 循環(huán)、do…while 循環(huán)和for 循環(huán)。本文將來和大家一起講講Java中這三個(gè)循環(huán)的使用,需要的可以參考一下2022-08-08
SpringBoot+mybatis+thymeleaf實(shí)現(xiàn)登錄功能示例
這篇文章主要介紹了SpringBoot+mybatis+thymeleaf實(shí)現(xiàn)登錄功能示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07

