簡體   English   中英

如何使用抽象類並實現它們

[英]how to use abstract classes, and implementing them

我不確定我能多么雄辯地真正解釋自己不理解/需要的幫助,我還是面向對象編程的新手。 這是關於我的課程的,我不希望有人為我做,我只需要幫助您了解如何繼續前進,即使我走上正確的道路。

好的,接下來我的問題。 基本上,我試圖創建一個數組列表,該數組列表將容納一些本身具有大量信息的對象(顯然),我的規范說是要創建一個抽象類,該類將由我的構造函數類進行擴展,就像我所做的那樣。 抽象類有一些變量(由spec決定),但是我不知道如何將它們移到我的擴展類中。

我將在下面發布我的代碼,並希望它有意義。 謝謝大家提供的任何幫助。 我現在很困惑。

基本上,我很想知道,A)我如何在數組列表中創建一個對象,該對象將能夠包含SportsClub和FootballClub中的所有內容,並且最好包含用戶輸入的所有變量。

B)我不知道如何打印對象,當我現在打印時,我得到了workwork.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創建一個構造函數,並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;
}

如果您使用設置器和獲取器,則還應將成員變量設置為protectedprivate

我不知道如何打印對象

您需要重寫toString 有一個簡短的教程這里


同樣無關的旁注:所有Java變量標識符均應以小寫字母開頭。

當您具有這樣的方法時:

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

它應該是:

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

要么:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM