簡體   English   中英

用Java實現多重繼承

[英]Implementing Multiple Inheritance in Java

我試圖弄清楚如何在我的java程序中實現多個接口。

我希望我的程序實現一個包含三個類(footballPlayer,學生和個人)的大學足球運動員類。

public class app {
public static void main(String[] args)
   { 
     student st1 = new student("Zack","Mills",22); 
     System.out.println(st1.getAllInfo()); 
     footballPlayer fp1 = new footballPlayer("Zack","Mills",22,5.9f, 240,"Junior","Running Back");
   System.out.println(fp1.getAllInfo());
   } 
 }

public class person {
 //---------Declaring attributes---- 
 private String firstName; 
 private String lastName; 
 private int age; 
 //------------------------------ 
 //----------Constructor------------ 
 person(String a, String b, int c) 
 { 
      firstName = a; 
      lastName = b; 
      age = c; 
 } 
 //---------- METHODS -------- 
 String getInfo() 
 { 
      return "NAME = "+getFirstName()+ " "+getLastName()+" "+"Age = "+ getAge(); 
 } 
 //------------------------------------------------ 
 /** 
  * @return the firstName 
  */ 
 public String getFirstName() { 
      return firstName; 
 } 
 /** 
  * @param firstName the firstName to set 
  */ 
 public void setFirstName(String firstName) { 
      this.firstName = firstName; 
 } 
 /** 
  * @return the lastName 
  */ 
 public String getLastName() { 
      return lastName; 
 } 
 /** 
  * @param lastName the lastName to set 
  */ 
 public void setLastName(String lastName) { 
      this.lastName = lastName; 
 } 
 /** 
  * @return the age 
  */ 
 public int getAge() { 
      return age; 
 } 
 /** 
  * @param age the age to set 
  */ 
 public void setAge(int age) { 
      this.age = age; 
  } 


}

public class footballPlayer extends person {

//-----------FOOTBALL PLAYER ATTRIBUTES----------------------------
private float height; 
private float weight; 
private String experience; 
private String position; 




footballPlayer(String fn, String ln, int ag,float ht, float wt, String exp, String pos) 
{ 
    super(fn, ln, ag); 
    height = ht; 
    weight = wt; 
    experience = exp; 
    position = pos; 
} 
/** 
 * @return the height 
 */ 
public float getHeight() { 
    return height; 
} 
/** 
 * @param height the height to set 
 */ 
public void setHeight(float height) { 
    this.height = height; 
} 
/** 
 * @return the weight 
 */ 
public float getWeight() { 
    return weight; 
} 
/** 
 * @param weight the weight to set 
 */ 
public void setWeight(float weight) { 
    this.weight = weight; 
} 
/** 
 * @return the experience 
 */ 
 public String getExperience() { 
     return experience; 
} 
/** 
 * @param experience the experience to set 
 */ 
public void setExperience(String experience) { 
     this.experience = experience; 
} 
/** 
 * @return the position 
 */ 
public String getPosition() { 
    return position; 
} 
/** 
 * @param position the position to set 
 */ 
public void setPosition(String position) { 
    this.position = position; 
} 
String getAllInfo() 
{ 
    return getFirstName() + " " + getLastName() + " " + getAge() + " " + " " + getHeight() + " " + getWeight() + " " + getExperience() + " " + getPosition(); 
} 

 private String status; 
}

public class student extends person {

private String status;
 student(String informedFirstName, String informedLastName, int informedAge) 
{ 
   super(informedFirstName, informedLastName, informedAge); 
   if (getAge() <= 25) status = "Traditional";

} 
String getStatus() 
{ 
   return this.status; 
} 
public void setStatus(String status) 
{ 
   this.status = status; 
} 

String getAllInfo() 
{ 
   return getFirstName() + " " + getLastName() + " " + getAge() + " " + getStatus(); 
 } 
}

 public class CollegefootballPlayer {

//attributes of football player and student

}

我也將添加評論,但是還沒有聲譽。 無論如何,其他人對於Java中缺乏多重繼承是正確的。 除了使用接口之外,還有一些關於對象組成而不是繼承的討論。

這是該主題的主題:

Java中組合優於繼承的優勢

合成具有很大的靈活性。 新類的成員對象通常是私有的,這使得使用該類的客戶端程序員無法訪問它們。 這使您可以更改那些成員而不會打擾現有的客戶端代碼。 您還可以在運行時更改成員對象,以動態更改程序的行為。 接下來描述的繼承不具有這種靈活性,因為編譯器必須對使用繼承創建的類施加編譯時限制。

因此,您可以維護一個帶有職業和愛好/體育等屬性列表的人員類別。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM