【问题标题】:how to use abstract classes, and implementing them如何使用抽象类并实现它们
【发布时间】: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;
    }

    }

【问题讨论】:

    标签: java class oop abstract


    【解决方案1】:

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

    【讨论】:

    • 非常感谢!你帮了大忙!而且我知道我的语法很糟糕,也感谢你帮助我学习正确的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 2013-03-11
    • 2020-10-20
    相关资源
    最近更新 更多