Java:将一个对象的字符串传递给另一个对象



我的帖子已经关闭,因为另一个问题的重复并不能真正解决我的问题,所以我再次发布它。因为这不是比较字符串,而是将一个对象的字符串传递给另一个对象

我试图通过编写一个代码来练习Java上的继承性,该代码由名为Person的Parent类组成,该类有两个子类SuperPersonivil,该类还有两个子类Hero和Villian。我试图实现一个名为Protect的方法,该方法仅由Hero类使用,该类只能在Civil作为目标的类上工作。所以,当我运行代码时,这里的主要问题是,当一个不同的英雄试图保护一个已经受到另一个英雄保护的公民时,以及当一个维廉试图攻击一个受到英雄保护的平民时,它没有显示保护人的名字。

主要方法:

Hero h1 = new Hero("Spiderman", 25,"Male", "Web");
Hero h2 = new Hero("Batman", 35, "Male","Wealth");
SuperPerson v1 = new Villian("Goblin", 47,"Male", "Goblin Power");
Person c3 = new Civil("Mary", 24,"Female");
h1.protect(c3); //First Hero object protecting the Civil object
h2.protect(c3);//Different Hero object trying to protect the same Civil object
v1.attack(c3);//Villian attacking civil under protection

输出:

Mary is under Spiderman's proction.
Mary is already under null's protection //Bug here
The target is under null's protection //Bug here

(个人(父类:

public class Person {
boolean protection;
}

(超人(父类:

public class SuperPerson extends Person {
String protector;
}

第一个有保护方法的孩子(英雄(类:

public class Hero extends SuperPerson {
void protect(Person target) {
if(target.type.equals("Super Villian")) {
System.out.println("You can not protect a villian!");
}
else {
if(target.health != 0) {
if(target.protection == false) {//to protect
target.protection = true;
this.protector = this.name;
System.out.println(target.name + " is under " + this.protector + "'s proction.");
}
else {
if(this.protector != this.name) {//under someone else protection so can not unprotect
System.out.println(target.name + " is already under " + protector + "'s protection");//Bug here
}
else {//to unprotect
target.protection = false;
System.out.println(target.name + " is no longer under " + this.name + "'s protection");
}
}
}
else {
System.out.println("Target is already dead.");
}
}
}
}

另一个孩子(维廉(班:

public class Villian extends SuperPerson{
int attack(Person target) {
if(target.type.equals(this.type)) {
System.out.println("Invalid target!");
return 0 ;
}
else {
if(target.protection == true) {
System.out.println("The target is under " + protector + "'s protection"); //Bug here
return 0;
}
else {
return super.attack(target);
}
}

}
}

当您调用时

h1.protect(c3);

您正在c3中存储它现在受到保护。

但是,您不是在c3中存储保护器的名称,而是在h1:中存储

this.protector = this.name;

这意味着当你稍后致电时

h2.protect(c3);

h2可以读取c3被保护但不能读取谁在保护c3

要解决此问题,您需要将字段String protector;设置为Person类的实例字段,并在其中存储保护程序的名称:

public class Person {
boolean protection;
String protector;
}

public class Hero extends SuperPerson {
void protect(Person target) {
if(target.type.equals("Super Villian")) {
System.out.println("You can not protect a villian!");
} else {
if (target.health != 0) {
if (!target.protection) {//to protect
target.protection = true;
target.protector = this.name;
System.out.println(target.name + " is under " + this.protector + "'s proction.");
} else {
if (!target.protector.equals(this.name)) {//under someone else protection so can not unprotect
System.out.println(target.name + " is already under " + target.protector + "'s protection");
} else {//to unprotect
target.protection = false;
System.out.println(target.name + " is no longer under " + this.name + "'s protection");
target.protector = null;
}
}
} else {
System.out.println("Target is already dead.");
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新