0

자바 프로그램에서 다중 인터페이스를 구현하는 방법을 알아 내려고하고 있습니다.Java에서 다중 상속 구현하기

내 프로그램에서 3 개의 클래스 (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 

} 
+1

자바에서 여러 인터페이스 만 구현할 수 있으며 여러 클래스를 확장 할 수 없습니다. 원하는 것을하기 위해 학생과 축구 선수를 인터페이스로 전환해야합니다. 관련이 없지만 코드를 들여 쓰고 Java 명명 규칙 (클래스 이름을 대문자로 사용)을 따르는 경우 코드가 읽기 쉽고 (따라서 더 쉽게 도움이 될 수 있습니다.) – nhouser9

+0

다중 인터페이스 구현! = 다중 상속 – azurefrog

+2

Java는 여러 객체/클래스에서 다중 상속을 지원하지 않습니다. 먼저 단일 클래스에서 확장하고 여러 개의 '인터페이스'를 구현할 수 있습니다. 만약 당신이 똑똑하다면 클래스를 체인으로 묶어서'Person' ->'Student' ->'FootballPlayer' – MadProgrammer

답변

1

덧글에 덧붙이 겠지만 아직 그렇게 할 명성이 없습니다. 어쨌든, 다른 것들은 자바에서 다중 상속의 부족에 대해 정확합니다. 인터페이스로 작업하는 것 외에도 상속 대신 오브젝트 구성에 대한 논의가 있습니다.

여기에 주제에 스레드의 :

Advantages of composition over inheritance in Java

구성 유연성의 큰 거래와 함께 제공됩니다. 새 클래스의 멤버 객체는 일반적으로 비공개이므로 클래스를 사용하는 클라이언트 프로그래머는 액세스 할 수 없게됩니다. 이렇게하면 기존 클라이언트 코드를 방해하지 않고 해당 구성원을 변경할 수 있습니다. 또한 런타임에 멤버 객체를 변경하여 프로그램의 비헤이비어를 동적으로 변경할 수 있습니다. 컴파일러는 상속을 통해 생성 된 클래스에 컴파일 타임 제한을 설정해야하기 때문에 다음에 설명하는 상속에는 이러한 유연성이 없습니다.

그래서 직업이나 취미/스포츠와 같은 속성 목록을 가진 사람 클래스를 유지할 수 있습니다.

+0

정확합니다. 사람/학생/축구 선수와의 예를 고려해 볼 때 더 나은 선택입니다. – Matt