자바 프로그램에서 다중 인터페이스를 구현하는 방법을 알아 내려고하고 있습니다.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
}
자바에서 여러 인터페이스 만 구현할 수 있으며 여러 클래스를 확장 할 수 없습니다. 원하는 것을하기 위해 학생과 축구 선수를 인터페이스로 전환해야합니다. 관련이 없지만 코드를 들여 쓰고 Java 명명 규칙 (클래스 이름을 대문자로 사용)을 따르는 경우 코드가 읽기 쉽고 (따라서 더 쉽게 도움이 될 수 있습니다.) – nhouser9
다중 인터페이스 구현! = 다중 상속 – azurefrog
Java는 여러 객체/클래스에서 다중 상속을 지원하지 않습니다. 먼저 단일 클래스에서 확장하고 여러 개의 '인터페이스'를 구현할 수 있습니다. 만약 당신이 똑똑하다면 클래스를 체인으로 묶어서'Person' ->'Student' ->'FootballPlayer' – MadProgrammer