我遇到了一些涉及访问超类中定义的变量的代码的问题,这些变量后来在子类中进行了修改。IE
//start of Stat class. Variables in question are defined here. (Name, Level, Experience, totalXP)
public class Stat {
public static String Name = " ";
public static int Level = 0;
public static double Experience = 0;
public static double totalXP = 0;
//Sets Name
public static void setName(String NameIn) {
Name = NameIn;
}
//Sets Level
public static void setLevel(int LevelIn) {
Level = LevelIn;
}
//Sets Experience
public static void setExperience(double ExperienceIn) {
Experience = ExperienceIn;
}
//Sets totalXP
public static void settotalXP(double totalXPIn) {
totalXP = totalXPIn;
}
//Sets up Attributes
public static void setall() {
setName("Herp");
setLevel(1);
setExperience(0);
settotalXP(0);
}
//Displays a Stat's Attributes
public static void DisplayLevel() {
System.out.println(Name);
System.out.println("Level: " + Level);
System.out.println(Experience + " out of " + totalXP + " total experience.");
}//End of method
public static void Levelup() {
if(Experience >= totalXP) {
Level++;
totalXP = totalXP * 1.3;
Experience = 0;
}//end of if statement
}//end of Levelup method
}//end of Stat.class
public class Agility extends Stat{
{//Revisionary Block
Name = "Agility";
Level = 1;
Experience = 0;
totalXP = 125;
}
public static int runnappDodge(int Dodge) {
Random generator = new Random(10);
Dodge = generator.nextInt(10);
if (Dodge == 0) {
Player.incomingDMG = 0;
}//end of if statement
return Dodge;
}
//start of the method located on player.class. This prints out " ", 0.0 and 0.0 for all //of the fields.
public static void checkLevel() {
System.out.println("You are level: " + Level);
System.out.println("You have " + experience + " experience out of " + totalXP + " total experience");
System.out.println(stat.Attack.Name + " Level: " + stat.Attack.Level + ": Experience: " + stat.Attack.Experience + "/" + stat.Attack.totalXP);
System.out.println(stat.Archery.Name +" Level: " + stat.Archery.Level +": Experience: " + stat.Archery.Experience + "/" + stat.Archery.totalXP);
System.out.println(stat.Agility.Name +" Level: " + stat.Agility.Level + ": Experience: " + stat.Agility.Experience + "/" + stat.Agility.totalXP);
}//end of checkLevel method
我的完整代码在这里: http: //pastebin.com/6nPGwJQe
reddit 没有帮助,所以我现在求助于你。(并不是说你没有帮助,但我只是更多地使用reddit,所以它更方便)。我认为我的代码应该更新子类中的变量,但它没有。当我引用该变量时,它最终完全是由超类定义的,在 Aaron.name 的情况下,是“”而不是“Aaron”。我不确定 getter 和 setter 在这里是否有用,但我感谢所有建议
PS:我关于stackoverflow的第一个问题!
编辑:感谢您的反馈。这是示例代码。最后一位评论者帮了很多忙,但我的真实代码完全独立于此。这是为了传达一个例子。因此,请查看链接,因为该代码确实很重要。编辑 2:因为我可以看到可能需要查看我的项目结构以进行导入和引用,这里是我的项目结构的图像:http: //imgur.com/VhDzRrm
编辑 3:我的帖子不再使用不正确的示例,而是使用我的实际代码。