簡體   English   中英

Java 從方法返回 null 值

[英]Java returning null value from method

我一直有一個 object 出現,因為Elf 1null應該顯示BEST FRIEND: HOBBIT 2但它只顯示null 我試圖尋找解決方案,但沒有任何幫助。 有人可以幫我找出解決方案嗎? 謝謝你。

例如 output:

ELF 1 [ STATUS: Alive Elf ] [ CLAN: Forest | BEST FRIEND: null ] [ STR: 5 | DEX: 7 | ARM: 1 | MOX: 16 ] [ COINS: 77 | HEALTH: 80 ]

ELF 2 [ STATUS: Alive Elf ] [ CLAN: City | BEST FRIEND: HOBBIT 1 ] [ STR: 14 | DEX: 20 | ARM: 18 | MOX: 12 ] [ COINS: 1000 | HEALTH: 80 ]

主class

        // HOBBIT 1 AND 2
        System.out.println("\nLet's create new two Hobbits!");
        System.out.println("Building two new hobbits...");
        System.out.println("\nHobbit 1 name: ");
        String hobbitName = sc.nextLine();
        Hobbit hobbit1 = new Hobbit(hobbitName);

        System.out.println("Hobbit 2 name: ");
        String hobbitName2 = sc.nextLine();
        Hobbit hobbit2 = new Hobbit(hobbitName2, 8, 12, 2, 14, 10, 30);
        System.out.println("\n" + hobbit1);
        System.out.println(hobbit2);

        // ELF 1 AND 2
        System.out.println("\nLet's create new two Elves!");
        System.out.println("Building two new Elves...");
        System.out.println("\nElf 1 name: ");
        String elfName1 = sc.nextLine();
        Elf elf1 = new Elf(elfName1);

        System.out.println("Elf 2 name: ");
        String elfName2 = sc.nextLine();
        Elf elf2 = new Elf(elfName2, 14, 20, 18, 12, 1000, 80, "Forest", hobbit1);
        System.out.println("\n" + elf1);
        System.out.println(elf2);

精靈 class 中的代碼


public class Elf extends Humanoid {

    private final String clan;    
    private String bestFriend;   

    public Elf(String name) {
        super(name);

        // randomly sets clan
        Random random = new Random();
        if(random.nextBoolean()){
            clan = "Forest";
        } else {
            clan = "City";
        }
    }

    public Elf(String name, String clan, Hobbit bestFriend) {
        super(name);
        this.clan = clan;
        this.bestFriend = bestFriend.getName();
    }

    public Elf(String name, int strength, int dexterity, int armour, int moxie, int coins, int healthRating, String clan, Hobbit bestFriend) {
        super(name, strength, dexterity, armour, moxie, coins, healthRating);
        this.clan = clan;
        this.bestFriend = bestFriend.getName();
    }

    public String getBestFriend() {
        return this.bestFriend;
    }

    public void setBestFriend(Hobbit bestFriend) {
        this.bestFriend = bestFriend.getName();
    }

    public String getClan() {
        return clan;
    }
    // only for elf 2 line
    @Override
    public String toString() {
        return getName() +
                " [ STATUS: " + super.isAlive() +
                " Elf ] [ CLAN: " + clan +
                " | BEST FRIEND: " + getBestFriend() +
                " ]" + super.toString();
    }
}```

您正在調用構造函數Elf(String)來創建 elf1; 此構造函數設置氏族和名稱,僅此而已。 沒有什么能設置“最好的朋友”,所以它仍然是 null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM