gpt4 book ai didi

java - 如何使用抽象类并实现它们

转载 作者:行者123 更新时间:2023-11-30 03:40:17 25 4
gpt4 key购买 nike

我不确定我能如何 Eloquent 地解释我不理解/需要帮助的内容,我对面向对象编程仍然很陌生。这是关于我的类(class)作业,我不希望有人为我做这件事,我只需要帮助了解如何继续前进,以及我是否走在正确的轨道上。

好的,接下来我的问题。基本上,我试图创建一个数组列表,它将保存一些对象,这些对象本身有一堆信息(显然),我的规范说创建一个抽象类,它将由我的构造函数类扩展,我就是这样做的。抽象类有一些变量(由规范决定),但我不知道如何将它们移到我的扩展类中。

我将在下面发布我的代码,希望它有意义。我将非常感谢你们能够提供的任何帮助。我现在很困惑。

基本上,我很想知道,A)如何在数组列表中创建一个对象,该对象将能够包含 SportsClub 和 FootballClub 中的所有内容,最好是用户输入的所有变量。

B)我不知道如何打印该对象,当我现在打印时,我得到了 coursework.FootballClub@49233bdc,我确信这是有原因的,但我需要显示对象中的信息,例如。姓名。如果可能的话,可以按名称的字母顺序对结果进行排序吗?我希望这一切都写好了。抱歉,提前谢谢您。

   package coursework;
import java.util.*;

/**
*
* @author w1469384
*/
public class PremierLeagueManager implements LeagueManager{
public static void main(String[] args) {
Scanner c1 = new Scanner(System.in);
Scanner c2 = new Scanner(System.in);
ArrayList<FootballClub> PL = new ArrayList<FootballClub>();
int choice;
System.out.println("Enter 1; To create a club, 2; To Delete a Club, 3; To display all clubs and 99 to close the program");
choice = c1.nextInt();
//Creates and adds a new FootballClub Object

while (choice != 99){
if (choice == 1){
System.out.println("Please Enter The games played for the club");
int played = c1.nextInt();
System.out.println("Please enter the number of wins");
int wins = c1.nextInt();
System.out.println("please enter the number of losses");
int losses = c1.nextInt();
System.out.println("please enter the number of draws");
int draws = c1.nextInt();
System.out.println("please enter the number of goals for");
int goalsFor = c1.nextInt();
System.out.println("please enter the number of goals against");
int goalsAgainst = c1.nextInt();
FootballClub club = new FootballClub(played, wins, losses, draws, goalsFor, goalsAgainst);
PL.add(club);
System.out.println("check");
}
//Deletes a FootballClub Object
if (choice == 2){

}
//Displays all Football Clubs in the PremierLeague array
if (choice == 3){

System.out.println(PL);
}
//Closes the Program 1

choice = c1.nextInt();



}
}
}

public abstract class SportsClub {
public String name;
public String location;
public int capacity;


public void setName(String Name){
name = Name;
}

public void setLocation(String Location){
location = Location;
}

public void setCapacity(int Capacity){
capacity = Capacity;
}

public String getName(){
return name;
}

public String getLocation(){
return location;
}

public int getCapacity(){
return capacity;
}
}

public class FootballClub extends SportsClub {
//Statistics for the club.
int played;
int wins;
int losses;
int draws;
int goalsFor;
int goalsAgainst;

public FootballClub(int gPlayed, int gWins, int gLosses, int gDraws, int gFor, int gAgainst){
played = gPlayed;
wins = gWins;
losses = gLosses;
draws = gDraws;
goalsFor = gFor;
goalsAgainst = gAgainst;
}


public void setPlayed(int newPlayed){
played = newPlayed;
}

public void setWins(int newWins){
wins = newWins;
}

public void setLosses(int newLosses){
losses = newLosses;
}

public void setDraws(int newDraws){
draws = newDraws;
}

public void setGoalsFor(int newGoalsFor){
goalsFor = newGoalsFor;
}

public void setGoalsAgainst(int newGoalsAgainst){
goalsAgainst = newGoalsAgainst;
}

public int getPlayed(){
return played;
}

public int getWins(){
return wins;
}

public int getLosses(){
return losses;
}

public int getDraws(){
return draws;
}

public int getGoalsFor(){
return goalsFor;
}

public int getGoalsAgainst(){
return goalsAgainst;
}

}

最佳答案

FootballClub 继承了 SportsClub 中声明的变量,因此您可以随意设置它们。

public FootballClub(
int gPlayed, int gWins, int gLosses, int gDraws, int gFor, int gAgainst,
String inName, String inLocation, int inCapacity
) {
played = gPlayed;
wins = gWins;
losses = gLosses;
draws = gDraws;
goalsFor = gFor;
goalsAgainst = gAgainst;

// set the variables from the superclass
name = inName;
location = inLocation;
capacity = inCapacity;
}

FootballClub 还继承了 SportsClub 中声明的方法,因此您也可以使用 setter 和 getter。

通常,您会为 SportsClub 创建一个构造函数来设置这些内容,然后从 FootballClub 构造函数调用该构造函数。

// in SportsClub
protected SportsClub(
String inName, String inLocation, int inCapacity
) {
name = inName;
location = inLocation;
capacity = inCapacity;
}

// in FootballClub
public FootballClub(
int gPlayed, int gWins, int gLosses, int gDraws, int gFor, int gAgainst,
String inName, String inLocation, int inCapacity
) {
super(inName, inLocation, inCapacity);

played = gPlayed;
wins = gWins;
losses = gLosses;
draws = gDraws;
goalsFor = gFor;
goalsAgainst = gAgainst;
}

您还应该将成员变量设为 protected or private如果您使用 setter 和 getter。

I don't know how to print The object

您需要重写toString。有一个简短的教程here .

<小时/>

另外一个不相关的旁注:所有 Java 变量标识符都应以小写字母开头。

当你有这样的方法时:

public void setName(String Name) { name = Name; }

应该是:

public void setName(String inName) { name = inName; }

或者:

public void setName(String name){ this.name = name; }

关于java - 如何使用抽象类并实现它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26964248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com