我不确定我能多么雄辩地真正解释自己不理解/需要的帮助,我还是面向对象编程的新手。这是关于我的课程的,我不希望有人为我做,我只需要帮助了解如何继续前进,即使我走在正确的道路上。

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

我将在下面发布我的代码,并希望它有意义。谢谢大家提供的任何帮助。我现在很困惑。

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

B)我不知道如何打印对象,当我现在打印时,我得到的是作业。FootballClub@ 49233bdc,我确定是有原因的,但是我需要对象中的信息才能显示,例如名称。如果可能的话,按照名称的字母顺序对结果进行排序?我希望一切都OK。对不起,谢谢你。

   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;
}


如果您使用的是setter和getter,则还应将成员变量设置为protected or private


  我不知道如何打印对象


您需要覆盖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; }

09-10 01:21
查看更多