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

Java: passing string of an object to another object

我的 post 已被关闭,因为另一个问题的重复并没有真正解决我的问题,所以我再次 post 解决这个问题。因为它不是关于比较字符串而是关于将 object 的字符串传递给另一个 objects.

我试图通过编写由 Parent class 组成的代码来练习 Java 的继承,称为 Person 具有两个 child class 的 SuperPersonCivil,以及 SuperPerson class 有两个 child classes 叫做 HeroVillian。 我试图实现一种名为 Protect 的方法,该方法仅供 Hero class 只能在 Civil class 作为目标。 所以,这里的主要问题是,当我 运行 代码时,它不会显示保护者的名字,以防不同的英雄试图保护已经在另一个英雄保护下的平民,而在恶棍试图保护的情况下攻击受英雄保护的平民。

主要方法:

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

(人)parent class:

public class Person {
    boolean protection;
}

The (SuperPerson) parent class:

public class SuperPerson extends Person {
    String protector;
}

第一个child(英雄)class有保护方法:

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.");
            }
        }
    }
}

另一个child(反派)class:

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 class 的实例字段,并将保护者的名称存储在那里:

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.");
            }
        }
    }
}