gpt4 book ai didi

java - 如何在我的演示代码中访问以下方法 "addPoints()"和 "getScores()"

转载 作者:行者123 更新时间:2023-12-02 06:16:08 25 4
gpt4 key购买 nike

我无法理解如何从类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 添加到基类中,以便能够从扩展它的类中调用它。

关于java - 如何在我的演示代码中访问以下方法 "addPoints()"和 "getScores()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55870915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com