这是我第一次来,我将尽力使这篇文章正确。我正在为我的Java类处理继承的练习。我有3个文件,DemoSugarSmash.java,SugarSmashPlayer.java和PremiumSugarSmashPlayer.java。
该程序对于SugarSmashPlayer部分可以正常工作,但是当尝试在PremiumSugarSmashPlayer.java文件中创建数组时,长度不应该超过10,而长度应为50。
这是练习的说明:
一个免费的名为Sugar Smash的在线游戏的开发人员要求您开发一个名为SugarSmashPlayer的类,该类可容纳有关单个玩家的数据。
该类包含以下字段:
玩家的整数ID号
字符串屏幕名称
一个整数数组,存储在10个游戏关卡中的每个关卡中获得的最高分数。
包括每个字段的获取和设置方法。得分的获取和设置方法应分别需要两个参数-一个代表获得的得分,另一个代表要获取或分配的游戏等级。如果用户尝试从超出分数阵列范围的级别分配或检索分数,则显示错误消息。
此外,除非用户在先前的每个级别上都获得了至少100分,否则除了第一个级别外都不能设置其他级别。如果用户尝试为尚不可用的水平设置分数,请发出错误消息。
编写从SugarSmashPlayer继承的类PremiumSugarSmashPlayer。当用户支付2.99美元来访问40个附加游戏级别时,将实例化此类。与游戏的免费版本一样,除非用户在所有先前级别上都获得了至少100分,否则用户无法为该级别设置分数。
我还没有为薪酬访问部分编码。
// extend SugarSmashPlayer as PremiumSugarSmashPlayer here
public class PremiumSugarSmashPlayer extends SugarSmashPlayer
{
// declare private variables here
private int levels = 50;
private int[] scores = new int[levels];
public PremiumSugarSmashPlayer()
{
// add constructor code here
}
// override (public!) getLevels() method here
@Override
public int getLevels()
{
return levels;
}
}
public class SugarSmashPlayer
{
// add private varaibles here
protected int IDNumber;
protected String screenName;
private int levels = 10;
private int[] scores = new int[levels];
public SugarSmashPlayer()
{
// add constructor code here
}
public void setIdNumber(int num)
{
// add method code here
IDNumber = num;
}
public void setName(String player)
{
// add method code here
screenName = player;
}
public void setScore(int score, int level)
{
// add method code here
boolean goodScores = false;
if (level == 0)
// Set score to index 0
scores[level] = score;
else
{
// Check if previous level score is greater than 100
if (scores[level - 1] > 100)
goodScores = true;
// Set score to game level
if (goodScores && level < scores.length)
scores[level] = score;
else
{
System.out.println("\nInvalid score");
}
}
}
public int getIdNumber()
{
// add method code here
return IDNumber;
}
public String getName()
{
// add method code here
return screenName;
}
public int getScore(int level)
{
// add method code here
if (level >= scores.length)
{
System.out.println("Invalid game level");
return -1;
}
else
return scores[level];
}
public int getMinScore()
{
// add method code here
return 100;
}
public int getLevels()
{
// add method code here
return levels;
}
}
import java.util.*;
public class DemoSugarSmash
{
public static void main(String[] args)
{
SugarSmashPlayer ssPlayer = new SugarSmashPlayer();
ssPlayer.setIdNumber(1111);
ssPlayer.setName("Alex");
System.out.println("\nAt start");
display(ssPlayer);
ssPlayer.setScore(200, 0);
System.out.println("\nAfter setting first score");
display(ssPlayer);
System.out.println("Trying to set fifth score too soon");
ssPlayer.setScore(30, 4);
System.out.println("\nAfter setting second score");
ssPlayer.setScore(30, 1);
display(ssPlayer);
System.out.println("\nTrying to set third score when second is too low");
ssPlayer.setScore(100, 2);
display(ssPlayer);
System.out.println("\nAfter setting second, third, fourth, and fifth scores");
ssPlayer.setScore(100, 1);
ssPlayer.setScore(300, 2);
ssPlayer.setScore(400, 3);
ssPlayer.setScore(10, 4);
display(ssPlayer);
System.out.println("\nTrying to set eleventh score");
ssPlayer.setScore(100, 10);
PremiumSugarSmashPlayer pssPlayer = new PremiumSugarSmashPlayer();
pssPlayer.setIdNumber(2222);
pssPlayer.setName("Cory");
System.out.println("\nAt start");
display(pssPlayer);
pssPlayer.setScore(200, 0);
System.out.println("\nAfter setting first score");
display(pssPlayer);
System.out.println("Trying to set fifth score too soon");
pssPlayer.setScore(30, 4);
System.out.println("\nAfter setting second score");
pssPlayer.setScore(30, 1);
display(pssPlayer);
System.out.println("\nTrying to set third score when second is too low");
pssPlayer.setScore(100, 2);
display(pssPlayer);
System.out.println("\nAfter setting second through tenth scores");
for(int x = 1; x < 10; ++x)
pssPlayer.setScore(130, x);
display(pssPlayer);
System.out.println("\nTrying to set eleventh score");
pssPlayer.setScore(100, 10);
display(pssPlayer);
System.out.println("\nTrying to set 51st score");
pssPlayer.setScore(100, 50);
display(pssPlayer);
}
public static void display(SugarSmashPlayer p)
{
System.out.println(" ID #" + p.getIdNumber() + " Name: " +
p.getName());
for(int x = 0; x < p.getLevels(); ++x)
System.out.print(" " + p.getScore(x));
System.out.println();
}
}
最佳答案
我相信您的问题是SugarSmashPlayer和PremiumSugarSmashPlayer各自都有自己的专用“得分”数组,但是作用于得分的方法(如setScore)仅存在于SugarSmashPlayer中,因此它们始终在SugarSmashPlayer的“得分”上运行。
要解决此问题,您将需要弄清楚如何仅在SugarSmashPlayer中具有“分数”,但是数组的大小会根据所使用的类而有所不同。
关于java - Java数组继承困惑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49502830/