【发布时间】:2014-11-17 01:39:16
【问题描述】:
我不确定我能多么雄辩地解释我不理解/需要帮助的内容,我对面向对象编程还是很陌生。这是关于我的课程作业,我不希望任何人为我做这件事,我只需要帮助了解如何继续前进,以及我是否走在正确的轨道上。
好的,继续我的问题。基本上,我正在尝试创建一个数组列表,它将包含一些对象,这些对象本身具有一堆信息(显然),我的规范说要创建一个抽象类,它将由我的构造函数类扩展,我这样做了。抽象类有一些变量(由规范决定)但我不知道如何将它们移到我的扩展类中。
我将在下面发布我的代码,希望它有意义。我会非常感谢你们能提供的任何帮助。我现在很困惑。
基本上,我很想知道,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;
}
}
【问题讨论】: