【问题标题】:Implementing Multiple Inheritance in Java在 Java 中实现多重继承
【发布时间】:2017-06-15 00:36:07
【问题描述】:

我试图弄清楚如何在我的 java 程序中实现多个接口。

我希望我的程序实现一个包含三个类(footballPlayer、student 和 person)的大学橄榄球运动员类。

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 命名约定(大写类名),您的代码将更易于阅读(因此我们更容易为您提供帮助)。
  • 实现多个接口!= 多重继承
  • 让我们开始吧,Java 不支持来自多个对象/类的多重继承,您可以从单个类和implement 多个interfaces 扩展。如果你很聪明,你可以将类链接起来,以便一个接一个地扩展 Person -> Student -> FootballPlayer
  • @MadProgrammer 反过来也会禁止 teacher 成为 FootballPlayer。
  • @Matt 也许这是一个合法的用例,教师不被允许成为足球运动员,也许他们被限制为只能被允许成为教练,但这需要更多的背景信息。就我个人而言,我会使用接口,然后你就不必关心(这么多)

标签: java object constructor abstract-class multiple-inheritance


【解决方案1】:

我也会添加到 cmets,但还没有这样做的声誉。无论如何,其他人对 Java 中缺乏多重继承是正确的。除了使用接口之外,还有一些关于对象组合而不是继承的讨论。

这是一个关于这个主题的线程:

Advantages of composition over inheritance in Java

组合具有很大的灵活性。新类的成员对象通常是私有的,使使用该类的客户端程序员无法访问它们。这允许您在不影响现有客户端代码的情况下更改这些成员。您还可以在运行时更改成员对象,以动态更改程序的行为。接下来将要介绍的继承没有这种灵活性,因为编译器必须对使用继承创建的类进行编译时限制。

因此,您可以维护一个包含职业和爱好/运动等属性列表的人员类。

【讨论】:

  • 没错。考虑到人/学生/足球运动员的例子,绝对是更好的选择
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 2016-10-02
相关资源
最近更新 更多