我的帖子已被關閉,因為另一個問題的重復并沒有真正解決我的問題,所以我再次發布。因為它不是關于比較字串,而是關于將一??個物件的字串傳遞給另一個物件。
我試圖通過撰寫一個代碼來練習 Java 上的繼承,該代碼由名為Person的父類組成,該類有兩個子類SuperPerson和Civil,以及SuperPerson類,它有兩個子類,稱為Hero和Villian。我試圖實作一個名為Protect的方法,該方法僅由只能在Civil 上作業的Hero類使用類作為目標。所以,當我運行代碼時,這里的主要問題是,在不同英雄試圖保護已經受到另一個英雄保護的平民的情況下,以及在惡棍試圖攻擊平民的情況下,它不會顯示保護者姓名誰在英雄的保護下。
主要方法:
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
(Person) 父類:
public class Person {
boolean protection;
}
(SuperPerson) 父類:
public class SuperPerson extends Person {
String protector;
}
具有保護方法的第一個子 (Hero) 類:
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.");
}
}
}
}
另一個孩子(Villian)類:
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);
}
}
}
}
uj5u.com熱心網友回復:
你打電話時
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.");
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/367107.html
上一篇:單詞之間用逗號
下一篇:用R變數的值替換字串的特定部分
