如何访问其他类中的变量?

时间:2016-10-23 23:16:04

标签: java variables bluej

我的名为Match的第一堂课创造了一个单独的足球/足球比赛。它让你选择2支球队和最终得分。名为“团队”的第二堂课更为先进。当调用游戏(匹配匹配)方法时,游戏的数量增加1.这部分工作正常。我的其他if语句(goalsForThisMatch)也可以。但是,当我检查Team类对象时,它应该显示与我在Match类中输入的目标相同的目标和目标。实际发生的是当我按下播放(匹配匹配)方法后检查Team类时,大多数方法都被设置为0,除了“播放”方法(按照它应该增加1)和最终得分是。因此,如果我在比赛课程中输入得分以便主队获得更多进球并赢得比赛,那么Team类的对象检查器中的Won方法将增加1.我需要其他两种方法在团队类中与匹配类链接。这些方法是:目标和目标。如果我将匹配类中的GoalsFor输入为“4”,那么当我检查Team类时,goalFor也应该设置为4。

我知道这一切可能听起来很混乱,请原谅我,我太累了,我准备睡觉了。希望在早上,有人会为我解决这个问题。

public class Match

    // instance variables - replace the example below with your own
    private String HomeTeam;
    private String AwayTeam;
    private int HomeGoals;
    private int AwayGoals;


    /**
     * Constructor for objects of class Match
     */
    public Match(String ShortHomeTeamName, String ShortAwayTeamName, int NewHomeGoals, int NewAwayGoals)
    {
        // initialise instance variables
        HomeTeam = ShortHomeTeamName;
        AwayTeam = ShortAwayTeamName;
        HomeGoals = NewHomeGoals;
        AwayGoals = NewAwayGoals;
    }

        public String getHomeTeamName(){

        return HomeTeam;
    }

        public String getAwayTeamName(){

        return AwayTeam;
    }

        public int getHomeGoals(){

        return HomeGoals;
    }    

        public int getAwayGoals(){

        return AwayGoals;
    }    


}




public class Team 

    private String TeamName;
    private String ShortName;
    private int Played;
    private int GoalsFor;
    private int GoalsAgainst;
    private int GoalDifference;
    private int Won;
    private int Drawn;
    private int Lost;
    private int Points;


    /**
     * Constructor for objects of class Team
     */
    public Team(String FullTeamName, String ShortTeamName)
    {
        // initialise instance variables
        TeamName = FullTeamName;
        ShortName = ShortTeamName;
        Played = 0;
        GoalsFor = 0;
        GoalsAgainst = 0;
        GoalDifference = 0;
        Won = 0;
        Drawn = 0;
        Lost = 0;
        Points = 0;       
    }

    public String getTeamName(){

        return TeamName;
    }

    public String getShortName(){

        return ShortName;
    }

    public int getPlayed(){

        return Played;
    }

    public void getGoalsFor(int InsertGoalsFor){

        GoalsFor = InsertGoalsFor;
    }

    public void getGoalsAgainst(int InsertGoalsAgainst){

        GoalsAgainst = InsertGoalsAgainst;

    }

    public int getGoalDifference(){


        return (GoalsFor - GoalsAgainst);


    }

    public int getWon(){

        return Won;
    }

    public int getDrawn(){

        return Drawn;
    }

    public int getLost(){

        return Lost;

    }    

    public int getPoints(){

        return Points;

    }

    public void play(Match match){

        Played++;

        int GoalsFor = match.getHomeGoals(); 
        int goalsForThisMatch = match.getHomeGoals();
        int goalsAgainstThisMatch = match.getAwayGoals();
        String homeTeam = match.getHomeTeamName();
        String ShortName = match.getHomeTeamName();

        if (ShortName.equals(TeamName)){

           ShortName = homeTeam; 

        } else {

           ShortName = match.getAwayTeamName();  

        }

        if (goalsForThisMatch > goalsAgainstThisMatch){

            Won++;

        }  

        else if (goalsForThisMatch == goalsAgainstThisMatch){

            Drawn++;

        }

        else {

            Lost++;


        }   


    }

} 

1 个答案:

答案 0 :(得分:0)

我相信您遇到的问题是,当您调用Team方法时,play类的字段未更新。

您看到此行为的原因是您在play方法中定义隐藏类成员变量的局部变量:

public void play(Match match){
    ...
    int GoalsFor = match.getHomeGoals(); 
    int goalsForThisMatch = match.getHomeGoals();
    int goalsAgainstThisMatch = match.getAwayGoals();
    String homeTeam = match.getHomeTeamName();
    String ShortName = match.getHomeTeamName();
    ...

此方法中定义的GoalsForShortName局部变量隐藏了您在类顶部定义的类成员变量:

public class Team 

    private String TeamName;
    private String ShortName;
    private int Played;
    private int GoalsFor;
    ...