Java:在它自己的方法中引用一个Class
Java: Referring to a Class inside its own method
我正在处理当前正在尝试调试的作业。我有很多error: (method name) in class (SuperClass Name) cannot be applied to given types;
。该项目正在将一个以过程编程为中心的游戏重新用于同一个游戏,但现在是围绕 OOP。我是 Java 的新手,负责创建许多 类,其中两个是 Superclass 的子class(很明显),我一直给定具有哪些方法及其参数。我遇到的问题是 subclass 中的一种方法应该控制两个角色(玩家和敌人)之间的战斗。这两个 classes 都是字符 class 的子class (superclass)。由于这是一项 class 任务,我不想 post 我拥有的一切,但下面是我尝试执行的战斗方法的示例。我遇到的问题,并继续与“inheritance”一般,是 exactly parent/child classes 以及如何在它们之间传递某些 values/variables。
在下面的示例代码中,此方法嵌套在扩展 Character
class 的 Player
class 中。此方法需要使用 Enemy
class 中的敌人并执行其中的操作。根据结果,我将一个布尔值传回我程序的主要方法。
我的问题是这样的;我不确定如何在这个例子中调用一个已经成为“Player
”class的class,在一个已经包含在“Player
”class。我被要求在调用时为该方法使用一个参数,Enemy
。我很肯定我没有以适当的方式处理这个特定的任务,并且有更好的方法来处理这个问题。但是非常感谢任何帮助理解什么是可能的,因为它将帮助我以正确的方式完成这项任务。
Player
class中的示例方法如下:
public abstract class Character{ //Character Superclass - attack method called
public int attack(Player _player){
Random randomNumbers = new Random();
int enemyRandAtk = randomNumbers.nextInt(Weapon.getMaxDamage - Weapon.getMinDamage) + Weapon.getMinDamage;
int enemyAtk = enemyRandAtk + getStrength();
int charRemainingHP = _player.getHitPoints() - enemyAtk; //can I call _player in the Character class????
System.out.printf("%s attacks with ATK = %d + %d = %d\n", getName(), getStrength(), enemyRandAtk, enemyAtk);
System.out.printf("%s HP is now %d - %d = %d\n\n", _player.getName(), _player.getHitPoints(), enemyAtk, charRemainingHP);
return charRemainingHP;
}
public class Player extends Character{
public int attack(Enemy _enemy){
Random randomNumbers = new Random();
int enemyHP = _enemy.getHitPoints();
int charRandAtk = randomNumbers.nextInt(Weapon.getMaxDamage - Weapon.getMinDamage) + Weapon.getMinDamage;
int charAtk = charRandAtk + getStrength();
int enemyRemainingHP = _enemy.getHitPoints() - charAtk;
System.out.printf("\n\n%s attacks with ATK = %d + %d = %d\n", getName(), getStrength(), charRandAtk, charAtk);
System.out.printf("%s HP is now %d - %d = %d\n\n", _enemy.getName(), enemyHP, charAtk, enemyRemainingHP);
return enemyRemainingHP;
}
public boolean battleWizard(Enemy _enemy){
Random randomNumbers = new Random();
Scanner userInput = new Scanner(System.in);
int spellCast = randomNumbers.nextInt(4) + 1;
System.out.printf("*** %s vs The Evil Wizard ***\n\n", getName()); //getName() is in Player Class
boolean charWinning = false;
int updWizHP = _enemy.getHitPoints(); //****** Question 1. below
do{
System.out.print("Choose your action: \n" +
"1. Attack\n" +
"2. Attempt Spell Cast\n\n" +
"What would you like to do?: ");
int battleDecision = userInput.nextInt();
if (battleDecision == 1){
updWizHP = Player.attack(_enemy); //**** Question #2 Below
if (updWizHP <= 0){
charWinning = true;
break;
}
}else if(battleDecision == 2){
System.out.print("Enter your guess: ");
int playerGuess = userInput.nextInt();
if (playerGuess == spellCast){
System.out.printf("The %s's spell is cast successfully! The Wizard's HP is now 0!\n\n", getName());
charWinning = true;
updWizHP = 0;
break;
}else{
System.out.print("Your spell missed the Wizard. Now he attacks!\n");
}
}
if (getHitPoints() > 0){
enemyDamage = Enemy.attack();
decreaseHitPoints(_enemy.attack(Player)); \**** Question #3 below
if (getHitPoints() < 0){
charWinning = false;
break;
}
}
}while(getHitPoints() > 0 && _enemy.getHitPoints() > 0);
if (charWinning)
return true;
else
return false;
}
}
请记住,这是子class (Player
) 中的一种方法,它与 (Enemy
) 中的方法大致相同,只是少了这个和一些其他
根据我上面的代码,这里是我的具体问题:
方法 battleWizard
中的第 6 行(注释中的#1 问题)- 由于此方法驻留在 Player
class 中,我可以引用 Enemy
class这样吗?如果不是,有什么更好的方法?
方法 battleWizard
中的第 12 行(注释中的#2 问题)- 当方法 (代码示例)在 class 本身?我想使用最终用户的播放器 player
并在其自身中引用该对象,如果这有意义的话?我正在尝试想象编译器将如何执行此任务并且无法想象它会起作用。
从底部算起第 13 行(注释中的#3 问题参考):
a) 你能像 Java 中这样将一个方法作为参数传递给另一个方法吗?
b) 是像这样调用另一个 subclass(与调用 class 相同的 subclass 级别)的正确方法,或者甚至可能吗?
感谢您的帮助。正如我之前提到的,由于这是一个 class 作业,所以我不想提供更多的代码示例。但不可避免地,如果它能帮助我理解,那么我会的。请让我知道您需要什么进一步的信息。
预计到达时间:
我添加了额外的代码来描述 super 和 subclasses 之间的关系。对于Enemy
subclass,其攻击方式依赖于Character
superclass。所以在我上面的代码示例中,我希望它能说明 3.b。我的问题。需要任何进一步的信息,请告诉我。
因此,Java 有 "visibility" 方法和字段(还有 classes),它定义了其他 class 究竟可以看到什么。查找 f.e。 JLS 6.5.7.1-1,简单方法名称和可见性。
但是,在您的情况下 "cannot be applied to given types" 意味着您传递给方法的参数类型与签名所说的不同。
不过,对于问题 2,您只需写 this.attack(_enemy)
,或者直接写 attack(_enemy)
。 (顺便说一句,下划线是什么?我希望它是转换过程中的产物,而不是您的风格指南中的内容)
问题三:只用_enemy.attack(this)
.
顺便说一句,您混淆了 OO 术语 - 传递了 classes 的实例。
您遇到编译错误,因为您定义了 attack(Player _player)
,这意味着您只允许传递 Player
个对象,但您使用的是 Player.attack(_enemy)
,这意味着传递 Enemy
对象。
您需要更正此问题。阅读我的段落,"As an aside..."。
Line 6 in method battleWizard (#1 Question in notes) - Since this
method resides in the Player class, can I reference the Enemy class in
this way? If not, what is a better way of doing so?
根据您的代码示例,如果您想获得敌人的生命值,int updWizHP = _enemy.getHitPoints();
是一个有效且明智的调用。 _enemy
是您的 Enemy 对象,您可以在其上使用任何方法,只要该方法存在于 class.
中即可
Line 12 in method battleWizard (#2 Question in notes) - How can I
reference a class that has an object created in it's own name when the
method (code example) is in the class itself? I'd like to take the
end-user's player player and reference that object within itself, if
that makes sense? I am trying to visualize how the compiler will
perform this task and can't imagine this ever working.
由于 Player
扩展了 Character
,因此您可以在 Player
class 中继承 attack
方法(可视化这是定义 attack
Character
class 中的方法)。所以,你真的不需要使用 updWizHP = Player.attack(_enemy);
但你可以简单地使用 updWizHP = attack(_enemy);
。但是,这是一个编译错误,请阅读我的第一部分和最后一部分答案。
现在,由于 attack
方法没有使用 Player
class 的任何实例字段(Player
class 的状态)所以你不需要担心,但如果是,那么 你必须考虑并决定 你想在哪个 Player
class 对象上调用你的 attack
方法。
13 Lines from the bottom (#3 Question reference in notes): a) can you
pass a method as a parameter to another method like this in Java? b)
is the proper way to invoke another subclass (same subclass level as
the one calling the class) like this or is it even possible?
- For #3.a: Using
decreaseHitPoints(_enemy.attack(Player));
你没有将方法传递给另一个方法,但首先 _enemy.attack(Player)
将被评估并作为你的代码 int
将被返回,该值将被传递给 decreaseHitPoints
.
- For #3.b: 你没有调用任何子class但是你在一个对象上调用方法,如果对象代表的 class 是否在继承树中。您唯一需要确保它是一个逻辑调用并且该方法存在于 class 中。
我猜
_enemy.attack(Player)
会给你编译错误,因为你没有将 Player
定义为对象引用变量。你必须使用一些变量,不能像这样使用 class 名称。最好使用 _enemy.attack(this)
_enemy.attack(Player)
这表明在攻击方法中你想传递一个 Player
对象,现在你要么使用 _enemy.attack(this)
,这意味着传递调用 battleWizard
的当前对象(make sense) 或使用 _enemy.attack(new Player())
这意味着创建一个 Player
class 的新对象并传递那个对象。
顺便说一句,我认为你最好将 public int attack(Player _player){
定义为 public int attack(Character _character){
因为像这样以后你可以使用 attack
方法来传递一个 Enemy
对象或 Character
的某些子 class
这就是"Inheritance"和"Program to interface"的美妙之处。我建议您搜索并了解这两个概念。
一些简短的笔记给你,你可能会意识到(这些是试图公开 topic/concept 的一般笔记,可能会有更多的限制行为,所以请详细阅读每个 topic/concept):
- 对象是 class.
的运行时表示
- super class 的方法被 sub-class 继承,如果你从同一个 class 调用它们,你不需要使用对象来调用它们。将其可视化为在同一 class 中定义该方法(阅读更多关于上下文中的继承 public v/s protected v/s 私有方法 )。
- 您可以覆盖继承的方法。
- 接受超类型的方法可以用其任何子类型的对象调用class。例如,在您的情况下
giveMeAnyCharacter(Character char)
可以用作 giveMeAnyCharacter(new Person())
或 giveMeAnyCharacter(new Enemy())
.
- 在Java中
this
表示当前对象或实例,super
表示超class。因此,要么创建一个新对象,要么如果您想使用您正在展示的同一对象,请使用 this
.
- 始终将所有公共代码(方法或实例字段)放在超级 class 中,让子 class 使用继承来利用它。
你的问题很不清楚,但我认为问题是你缺乏术语以及缺乏理解。我会为您尝试一些建议:
Line 6 in method battleWizard - Since this method resides in the
Player class, can I reference the Enemy class in this way? If not,
what is a better way of doing so?
int updWizHP = _enemy.getHitPoints(); //****** Question 1. below
如果 getHitPoints()
在 Player
class 中,您不能用 Enemy
实例 调用它。字段和方法必须存在于调用中使用的实例的 classes 中,或其继承树中(超级 classes)。
如果 getHitPoints()
对 Player
和 Enemy
都是通用的,您应该将方法放在最接近两者通用的 class 中 - 在您的情况下,它将是 Character
class。将方法放在字符 class 中(并赋予它 protected 或 public 可见性)允许它同时存在于Player
和 Enemy
classes(并使用 Player 和 Enemy 的关联实例调用)
How can I reference a class that has an object created in it's own
name when the method (code example) is in the class itself?
我唯一能想到的是您在这里描述的是 this 关键字(您似乎没有在代码中使用它)。 this
表示您所在 class 的当前实例。
所以使用this.attack(_enemy)
就是你如何让当前Player
实例攻击指定的敌人。
a) can you pass a method as a parameter to another method like this in
Java? b) is the proper way to invoke another subclass (same subclass
level as the one calling the class) like this or is it even possible?
a) 否。Java 不允许将方法作为参数传递。如果你想传递一个方法,你必须传递一个 instance (属于包含该方法的 class )作为变量 - 比如 x
- 然后调用您想要作为 x.method()
执行的方法。您正在做的是调用一个方法并使用return值作为参数,这是完全允许的。
decreaseHitPoints(_enemy.attack); // passing a method - not allowed
decreaseHitPoints(_enemy.attack(Player)); // passing a return value from calling a method - OK
b) 我想你想要的是 this.decreaseHitPoints(_enemy.attack(this));
。是的,您可以(并且应该)调用其他 classes 中的方法 - 但如果您的调用 class 是不相关的(即不是继承自),您只能调用那些已用 public 可见度。
我正在处理当前正在尝试调试的作业。我有很多error: (method name) in class (SuperClass Name) cannot be applied to given types;
。该项目正在将一个以过程编程为中心的游戏重新用于同一个游戏,但现在是围绕 OOP。我是 Java 的新手,负责创建许多 类,其中两个是 Superclass 的子class(很明显),我一直给定具有哪些方法及其参数。我遇到的问题是 subclass 中的一种方法应该控制两个角色(玩家和敌人)之间的战斗。这两个 classes 都是字符 class 的子class (superclass)。由于这是一项 class 任务,我不想 post 我拥有的一切,但下面是我尝试执行的战斗方法的示例。我遇到的问题,并继续与“inheritance”一般,是 exactly parent/child classes 以及如何在它们之间传递某些 values/variables。
在下面的示例代码中,此方法嵌套在扩展 Character
class 的 Player
class 中。此方法需要使用 Enemy
class 中的敌人并执行其中的操作。根据结果,我将一个布尔值传回我程序的主要方法。
我的问题是这样的;我不确定如何在这个例子中调用一个已经成为“Player
”class的class,在一个已经包含在“Player
”class。我被要求在调用时为该方法使用一个参数,Enemy
。我很肯定我没有以适当的方式处理这个特定的任务,并且有更好的方法来处理这个问题。但是非常感谢任何帮助理解什么是可能的,因为它将帮助我以正确的方式完成这项任务。
Player
class中的示例方法如下:
public abstract class Character{ //Character Superclass - attack method called
public int attack(Player _player){
Random randomNumbers = new Random();
int enemyRandAtk = randomNumbers.nextInt(Weapon.getMaxDamage - Weapon.getMinDamage) + Weapon.getMinDamage;
int enemyAtk = enemyRandAtk + getStrength();
int charRemainingHP = _player.getHitPoints() - enemyAtk; //can I call _player in the Character class????
System.out.printf("%s attacks with ATK = %d + %d = %d\n", getName(), getStrength(), enemyRandAtk, enemyAtk);
System.out.printf("%s HP is now %d - %d = %d\n\n", _player.getName(), _player.getHitPoints(), enemyAtk, charRemainingHP);
return charRemainingHP;
}
public class Player extends Character{
public int attack(Enemy _enemy){
Random randomNumbers = new Random();
int enemyHP = _enemy.getHitPoints();
int charRandAtk = randomNumbers.nextInt(Weapon.getMaxDamage - Weapon.getMinDamage) + Weapon.getMinDamage;
int charAtk = charRandAtk + getStrength();
int enemyRemainingHP = _enemy.getHitPoints() - charAtk;
System.out.printf("\n\n%s attacks with ATK = %d + %d = %d\n", getName(), getStrength(), charRandAtk, charAtk);
System.out.printf("%s HP is now %d - %d = %d\n\n", _enemy.getName(), enemyHP, charAtk, enemyRemainingHP);
return enemyRemainingHP;
}
public boolean battleWizard(Enemy _enemy){
Random randomNumbers = new Random();
Scanner userInput = new Scanner(System.in);
int spellCast = randomNumbers.nextInt(4) + 1;
System.out.printf("*** %s vs The Evil Wizard ***\n\n", getName()); //getName() is in Player Class
boolean charWinning = false;
int updWizHP = _enemy.getHitPoints(); //****** Question 1. below
do{
System.out.print("Choose your action: \n" +
"1. Attack\n" +
"2. Attempt Spell Cast\n\n" +
"What would you like to do?: ");
int battleDecision = userInput.nextInt();
if (battleDecision == 1){
updWizHP = Player.attack(_enemy); //**** Question #2 Below
if (updWizHP <= 0){
charWinning = true;
break;
}
}else if(battleDecision == 2){
System.out.print("Enter your guess: ");
int playerGuess = userInput.nextInt();
if (playerGuess == spellCast){
System.out.printf("The %s's spell is cast successfully! The Wizard's HP is now 0!\n\n", getName());
charWinning = true;
updWizHP = 0;
break;
}else{
System.out.print("Your spell missed the Wizard. Now he attacks!\n");
}
}
if (getHitPoints() > 0){
enemyDamage = Enemy.attack();
decreaseHitPoints(_enemy.attack(Player)); \**** Question #3 below
if (getHitPoints() < 0){
charWinning = false;
break;
}
}
}while(getHitPoints() > 0 && _enemy.getHitPoints() > 0);
if (charWinning)
return true;
else
return false;
}
}
请记住,这是子class (Player
) 中的一种方法,它与 (Enemy
) 中的方法大致相同,只是少了这个和一些其他
根据我上面的代码,这里是我的具体问题:
方法
battleWizard
中的第 6 行(注释中的#1 问题)- 由于此方法驻留在Player
class 中,我可以引用Enemy
class这样吗?如果不是,有什么更好的方法?方法
battleWizard
中的第 12 行(注释中的#2 问题)- 当方法 (代码示例)在 class 本身?我想使用最终用户的播放器player
并在其自身中引用该对象,如果这有意义的话?我正在尝试想象编译器将如何执行此任务并且无法想象它会起作用。从底部算起第 13 行(注释中的#3 问题参考): a) 你能像 Java 中这样将一个方法作为参数传递给另一个方法吗? b) 是像这样调用另一个 subclass(与调用 class 相同的 subclass 级别)的正确方法,或者甚至可能吗?
感谢您的帮助。正如我之前提到的,由于这是一个 class 作业,所以我不想提供更多的代码示例。但不可避免地,如果它能帮助我理解,那么我会的。请让我知道您需要什么进一步的信息。
预计到达时间:
我添加了额外的代码来描述 super 和 subclasses 之间的关系。对于Enemy
subclass,其攻击方式依赖于Character
superclass。所以在我上面的代码示例中,我希望它能说明 3.b。我的问题。需要任何进一步的信息,请告诉我。
因此,Java 有 "visibility" 方法和字段(还有 classes),它定义了其他 class 究竟可以看到什么。查找 f.e。 JLS 6.5.7.1-1,简单方法名称和可见性。
但是,在您的情况下 "cannot be applied to given types" 意味着您传递给方法的参数类型与签名所说的不同。
不过,对于问题 2,您只需写 this.attack(_enemy)
,或者直接写 attack(_enemy)
。 (顺便说一句,下划线是什么?我希望它是转换过程中的产物,而不是您的风格指南中的内容)
问题三:只用_enemy.attack(this)
.
顺便说一句,您混淆了 OO 术语 - 传递了 classes 的实例。
您遇到编译错误,因为您定义了 attack(Player _player)
,这意味着您只允许传递 Player
个对象,但您使用的是 Player.attack(_enemy)
,这意味着传递 Enemy
对象。
您需要更正此问题。阅读我的段落,"As an aside..."。
Line 6 in method battleWizard (#1 Question in notes) - Since this method resides in the Player class, can I reference the Enemy class in this way? If not, what is a better way of doing so?
根据您的代码示例,如果您想获得敌人的生命值,int updWizHP = _enemy.getHitPoints();
是一个有效且明智的调用。 _enemy
是您的 Enemy 对象,您可以在其上使用任何方法,只要该方法存在于 class.
Line 12 in method battleWizard (#2 Question in notes) - How can I reference a class that has an object created in it's own name when the method (code example) is in the class itself? I'd like to take the end-user's player player and reference that object within itself, if that makes sense? I am trying to visualize how the compiler will perform this task and can't imagine this ever working.
由于 Player
扩展了 Character
,因此您可以在 Player
class 中继承 attack
方法(可视化这是定义 attack
Character
class 中的方法)。所以,你真的不需要使用 updWizHP = Player.attack(_enemy);
但你可以简单地使用 updWizHP = attack(_enemy);
。但是,这是一个编译错误,请阅读我的第一部分和最后一部分答案。
现在,由于 attack
方法没有使用 Player
class 的任何实例字段(Player
class 的状态)所以你不需要担心,但如果是,那么 你必须考虑并决定 你想在哪个 Player
class 对象上调用你的 attack
方法。
13 Lines from the bottom (#3 Question reference in notes): a) can you pass a method as a parameter to another method like this in Java? b) is the proper way to invoke another subclass (same subclass level as the one calling the class) like this or is it even possible?
- For #3.a: Using
decreaseHitPoints(_enemy.attack(Player));
你没有将方法传递给另一个方法,但首先_enemy.attack(Player)
将被评估并作为你的代码int
将被返回,该值将被传递给decreaseHitPoints
. - For #3.b: 你没有调用任何子class但是你在一个对象上调用方法,如果对象代表的 class 是否在继承树中。您唯一需要确保它是一个逻辑调用并且该方法存在于 class 中。
我猜
_enemy.attack(Player)
会给你编译错误,因为你没有将Player
定义为对象引用变量。你必须使用一些变量,不能像这样使用 class 名称。最好使用_enemy.attack(this)
_enemy.attack(Player)
这表明在攻击方法中你想传递一个 Player
对象,现在你要么使用 _enemy.attack(this)
,这意味着传递调用 battleWizard
的当前对象(make sense) 或使用 _enemy.attack(new Player())
这意味着创建一个 Player
class 的新对象并传递那个对象。
顺便说一句,我认为你最好将
public int attack(Player _player){
定义为 public int attack(Character _character){
因为像这样以后你可以使用 attack
方法来传递一个 Enemy
对象或 Character
的某些子 class
这就是"Inheritance"和"Program to interface"的美妙之处。我建议您搜索并了解这两个概念。
一些简短的笔记给你,你可能会意识到(这些是试图公开 topic/concept 的一般笔记,可能会有更多的限制行为,所以请详细阅读每个 topic/concept):
- 对象是 class. 的运行时表示
- super class 的方法被 sub-class 继承,如果你从同一个 class 调用它们,你不需要使用对象来调用它们。将其可视化为在同一 class 中定义该方法(阅读更多关于上下文中的继承 public v/s protected v/s 私有方法 )。
- 您可以覆盖继承的方法。
- 接受超类型的方法可以用其任何子类型的对象调用class。例如,在您的情况下
giveMeAnyCharacter(Character char)
可以用作giveMeAnyCharacter(new Person())
或giveMeAnyCharacter(new Enemy())
. - 在Java中
this
表示当前对象或实例,super
表示超class。因此,要么创建一个新对象,要么如果您想使用您正在展示的同一对象,请使用this
. - 始终将所有公共代码(方法或实例字段)放在超级 class 中,让子 class 使用继承来利用它。
你的问题很不清楚,但我认为问题是你缺乏术语以及缺乏理解。我会为您尝试一些建议:
Line 6 in method battleWizard - Since this method resides in the Player class, can I reference the Enemy class in this way? If not, what is a better way of doing so?
int updWizHP = _enemy.getHitPoints(); //****** Question 1. below
如果 getHitPoints()
在 Player
class 中,您不能用 Enemy
实例 调用它。字段和方法必须存在于调用中使用的实例的 classes 中,或其继承树中(超级 classes)。
如果 getHitPoints()
对 Player
和 Enemy
都是通用的,您应该将方法放在最接近两者通用的 class 中 - 在您的情况下,它将是 Character
class。将方法放在字符 class 中(并赋予它 protected 或 public 可见性)允许它同时存在于Player
和 Enemy
classes(并使用 Player 和 Enemy 的关联实例调用)
How can I reference a class that has an object created in it's own name when the method (code example) is in the class itself?
我唯一能想到的是您在这里描述的是 this 关键字(您似乎没有在代码中使用它)。 this
表示您所在 class 的当前实例。
所以使用this.attack(_enemy)
就是你如何让当前Player
实例攻击指定的敌人。
a) can you pass a method as a parameter to another method like this in Java? b) is the proper way to invoke another subclass (same subclass level as the one calling the class) like this or is it even possible?
a) 否。Java 不允许将方法作为参数传递。如果你想传递一个方法,你必须传递一个 instance (属于包含该方法的 class )作为变量 - 比如 x
- 然后调用您想要作为 x.method()
执行的方法。您正在做的是调用一个方法并使用return值作为参数,这是完全允许的。
decreaseHitPoints(_enemy.attack); // passing a method - not allowed
decreaseHitPoints(_enemy.attack(Player)); // passing a return value from calling a method - OK
b) 我想你想要的是 this.decreaseHitPoints(_enemy.attack(this));
。是的,您可以(并且应该)调用其他 classes 中的方法 - 但如果您的调用 class 是不相关的(即不是继承自),您只能调用那些已用 public 可见度。