简体   繁体   English

用Java实现多重继承

[英]Implementing Multiple Inheritance in Java

I'm trying to figure out how to implement multiple interfaces in my java program. 我试图弄清楚如何在我的java程序中实现多个接口。

I want my program to implement a college football player class that incorporates the three classes (footballPlayer, student and person). 我希望我的程序实现一个包含三个类(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

}

I would add to the comments as well but don't have the reputation to do so yet. 我也将添加评论,但是还没有声誉。 Anyway, the others are correct about lack of multiple inheritance in Java. 无论如何,其他人对于Java中缺乏多重继承是正确的。 In addition to working with interfaces there is also some discussion about object composition instead of inheritance. 除了使用接口之外,还有一些关于对象组成而不是继承的讨论。

Here's a thread on the subject: 这是该主题的主题:

Advantages of composition over inheritance in Java Java中组合优于继承的优势

Composition comes with a great deal of flexibility. 合成具有很大的灵活性。 The member objects of your new class are typically private, making them inaccessible to the client programmers who are using the class. 新类的成员对象通常是私有的,这使得使用该类的客户端程序员无法访问它们。 This allows you to change those members without disturbing existing client code. 这使您可以更改那些成员而不会打扰现有的客户端代码。 You can also change the member objects at run time, to dynamically change the behavior of your program. 您还可以在运行时更改成员对象,以动态更改程序的行为。 Inheritance, which is described next, does not have this flexibility since the compiler must place compile-time restrictions on classes created with inheritance. 接下来描述的继承不具有这种灵活性,因为编译器必须对使用继承创建的类施加编译时限制。

So you could maintain a person class with a list of attributes like occupation and hobbies/sports. 因此,您可以维护一个带有职业和爱好/体育等属性列表的人员类别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM