【发布时间】: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