我无法理解如何从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函数添加到基类中,以便能够从扩展它的类中调用它。
获取正在运行的JAR文件的路径将返回“ rsrc:./” - java我的代码在JAR文件中运行,我需要获取该文件的完整路径。例如,我的JAR名为example.jar,位于D:\ example \因此,我需要通过该jar内的一些代码来获取“ D:\ example \ example.jar”。我尝试了许多方法来获取该路径,但是没有一种方法能正常工作。其中之一是getClass().getProtectionDomain(…
java:继承 - java有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…
Java:BigInteger,如何通过OutputStream编写它 - java我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…
Java-如何将此字符串转换为日期? - java我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…
Java:从类中查找项目名称 - java仅通过类的实例,如何使用Java反射或类似方法查找项目名称?如果不是,项目名称(我真正想要的是)可以找到程序包名称吗? 参考方案 项目只是IDE使用的简单组织工具,因此项目名称不是类或JVM中包含的信息。要获取软件包,请使用Class#getPackage()。然后,可以调用Package#getName()将包作为您在代码的包声明中看到的String来获取…