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

时间:2014-11-17 01:39:17

标签: java class oop abstract

我不确定我能够如何雄辩地解释我不理解/需要帮助的东西,我仍然是面向对象编程的新手。这是关于我的课程作业,我不希望任何人为我做这些,我只需要帮助了解如何继续前进,如果我甚至在正确的轨道上。

好的,等我的问题。基本上,我试图创建一个arraylist,它将包含一些本身有一堆信息的对象(显然),我的规范说要创建一个抽象类,它将由我的构造函数类扩展。抽象类有一些变量(由spec决定)但我不知道如何将它们移到我的扩展类中。

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

基本上,我很想知道,A)如何在我的arraylist中创建一个对象,它可以包含SportsClub和FootballClub中的所有内容,最好是用户输入的所有变量。

而且B)我不知道如何打印对象,当我现在打印时,我得到了课程作业.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;
    }

    }

1 个答案:

答案 0 :(得分:2)

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