我无法理解如何从RatingCalculator类访问方法getScore()以及如何从CalculatorForBoxing类访问重写的方法addPoints()。

public class Calculator {
    class Score{
        int score;
        int playerId;
    }

    class RatingCalculator extends Score {
        ArrayList<CalculatorForBoxing> newGame;
        public CalculatorForBoxing boxer1;
        public CalculatorForBoxing boxer2;


        ArrayList<Integer> getScores(){
            return myArrayList;
        }
    }


这是特定运动的计算器

     class CalculatorForBoxing extends RatingCalculator implements RateByAccumulatingPoints {

        int forbiddenKicks;
        int successfullKicks;

        public  CalculatorForBoxing  (int playerId, int score, int forbiddenKicks, int successfullKicks ) {
            this.playerId = playerId;
            this.score = score;
            this.forbiddenKicks = forbiddenKicks;
            this.successfullKicks = successfullKicks;
            }
        public void setPlayerId(int playerId) {
            this.playerId = playerId;
        }

        public int getPlayerId() {
            return playerId;
        }
        public void setScore(int score) {
            this.score = score;
        }
        public int getScore() {
            return score;
        }

        @Override
        public void addPoints(int playerId, int points) {
            //some code
            }
        }
    interface RateByAccumulatingPoints {
         void addPoints(int playerId, int points );
        }


在这里,我需要证明计算器如何适用于各种运动

    class Judge extends RatingCalculator  {

        // here my simple scenario;
        void rate(RatingCalculator rc){
            newGame = new ArrayList<CalculatorForBoxing>();
            newGame.add(boxer1 = new CalculatorForBoxing(01, 0, 0, 4));
            newGame.add(boxer2 = new CalculatorForBoxing(02, 0, 0, 5));
            newGame.addPoints(01, 20); //haven't access here
            newGame.getScores();       //haven't access here
        }
    }

最佳答案

在代码中newGame是一个数组列表,而不是类对象,这就是为什么您无法调用addPoints和getScores的原因。

在这种情况下,您只需要像在基类中那样简单地调用getScores()即可。但是,您需要将addPoints函数添加到基类中,以便能够从扩展它的类中调用它。

07-24 20:42