欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Java使用super和this來重載構(gòu)造方法

 更新時(shí)間:2017年08月21日 11:10:30   投稿:lqh  
這篇文章主要介紹了詳解Java使用super和this來重載構(gòu)造方法的相關(guān)資料,這里提供實(shí)例來幫助大家理解這部分內(nèi)容,需要的朋友可以參考下

詳解Java使用super和this來重載構(gòu)造方法

實(shí)例代碼:

//父類 
class anotherPerson{ 
  String name = ""; 
  String age = ""; 
  public String getAge() { 
    return age; 
  } 
  public void setAge(String age) { 
    this.age = age; 
  } 
  public void setName(String name){ 
    this.name = name; 
  } 
  public String getName(){ 
    return name; 
  } 
  //第一個(gè)構(gòu)造方法 
  public anotherPerson (String name){ 
    this.name = name; 
  } 
  //第二個(gè)構(gòu)造方法 
  public anotherPerson(String name, String age){ 
    this(name);//是用同一類中的其他構(gòu)造方法 
    this.age = age; 
  } 
   
  public void ShowInfomation(){ 
    System.out.println("name is "+ name +"and age is "+age); 
  } 
} 
//子類 
class Teacher extends anotherPerson{ 
  String school = ""; 
  public void setSchool(String school){ 
    this.school = school; 
  } 
  public String getSchool(){ 
    return school; 
  } 
  public Teacher(String name){ 
    super(name); 
  } 
  //第一個(gè)構(gòu)造方法 
  public Teacher(String age,String school){ 
    super("babyDuncan",age);//使用父類的構(gòu)造方法 
    this.school = school; 
  } 
  public Teacher(String name,String age,String school){ 
    this(age,school);//使用同一類的構(gòu)造方法,而這一構(gòu)造方法使用父類的構(gòu)造方法 
    this.name = name; 
  } 
  //重寫了父類的函數(shù) 
  public void ShowInfomation(){ 
    System.out.println("name is "+ name +" and age is "+age+" and school is "+school); 
  } 
} 
public class testTeacher { 
 
  /** 
   * 測試一下super和this 
   */ 
  public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    anotherPerson person1 = new anotherPerson("babyDuncan"); 
    anotherPerson person2 = new anotherPerson("babyDuncan","20"); 
    Teacher teacher1 = new Teacher("babyDuncan"); 
    Teacher teacher2 = new Teacher("20","JLU"); 
    Teacher teacher3 = new Teacher("babyDuncan","20","JLU"); 
    person1.ShowInfomation(); 
    person2.ShowInfomation(); 
    teacher1.ShowInfomation(); 
    teacher2.ShowInfomation(); 
    teacher3.ShowInfomation(); 
  } 
 
} 

輸出結(jié)果:

name is babyDuncanand age is 
name is babyDuncanand age is 20 
name is babyDuncan and age is and school is 
name is babyDuncan and age is 20 and school is JLU 
name is babyDuncan and age is 20 and school is JLU

以上就是java this與super的實(shí)例應(yīng)用,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • SpringMVC獲取請求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的示例代碼

    SpringMVC獲取請求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的示例代碼

    這篇文章主要給大家介紹了SpringMVC獲取請求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的示例代碼,文中通過代碼示例給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
    2023-12-12
  • Sax解析xml_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Sax解析xml_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要介紹了Sax解析xml,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • JAVA實(shí)現(xiàn)心跳檢測(長連接)

    JAVA實(shí)現(xiàn)心跳檢測(長連接)

    本文主要介紹了JAVA實(shí)現(xiàn)心跳檢測(長連接),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • Java項(xiàng)目防止SQL注入的幾種方式

    Java項(xiàng)目防止SQL注入的幾種方式

    SQL注入是一種常見的攻擊方式,黑客試圖通過操縱應(yīng)用程序的輸入來執(zhí)行惡意SQL查詢,從而繞過認(rèn)證和授權(quán),竊取、篡改或破壞數(shù)據(jù)庫中的數(shù)據(jù),本文主要介紹了Java項(xiàng)目防止SQL注入的幾種方式,感興趣的可以了解一下
    2023-12-12
  • MyBatis-Plus 查詢指定字段的實(shí)現(xiàn)

    MyBatis-Plus 查詢指定字段的實(shí)現(xiàn)

    這篇文章主要介紹了MyBatis-Plus 查詢指定字段的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • 使用Easyexcel實(shí)現(xiàn)不同場景的數(shù)據(jù)導(dǎo)出功能

    使用Easyexcel實(shí)現(xiàn)不同場景的數(shù)據(jù)導(dǎo)出功能

    這篇文章主要為大家詳細(xì)介紹了如何在不同場景下使用Easyexcel實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • Java中的Excel框架使用詳解

    Java中的Excel框架使用詳解

    這篇文章主要介紹了Java中的Excel框架使用詳解,Java解析、生成Excel比較有名的框架有Apache poi、jxl,但他們都存在一個(gè)嚴(yán)重的問題就是非常的耗內(nèi)存,poi有一套SAX模式的API可以一定程度的解決一些內(nèi)存溢出的問題,需要的朋友可以參考下
    2023-11-11
  • Java多線程阻塞與喚醒代碼示例

    Java多線程阻塞與喚醒代碼示例

    本文主要向大家分享了Java多線程中的阻塞與喚醒的相關(guān)內(nèi)容,通過這篇文章大家可以大致了解到進(jìn)入線程阻塞狀態(tài)和可執(zhí)行狀態(tài)的方法,需要的朋友可以了解下。
    2017-09-09
  • Java面向?qū)ο蟪绦蛟O(shè)計(jì):抽象類,接口用法實(shí)例分析

    Java面向?qū)ο蟪绦蛟O(shè)計(jì):抽象類,接口用法實(shí)例分析

    這篇文章主要介紹了Java面向?qū)ο蟪绦蛟O(shè)計(jì):抽象類,接口用法,結(jié)合實(shí)例形式分析了java抽象類與接口相關(guān)概念、原理、用法與操作注意事項(xiàng),需要的朋友可以參考下
    2020-04-04
  • springboot接收http請求,解決參數(shù)中+號變成空格的問題

    springboot接收http請求,解決參數(shù)中+號變成空格的問題

    這篇文章主要介紹了springboot接收http請求,解決參數(shù)中+號變成空格的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評論