Java 从方法返回 null 值

huangapple go评论75阅读模式
英文:

Java returning null value from method

问题

ELF 1 [ STATUS: 活着的精灵 ] [ CLAN: 森林 | BEST FRIEND: null ] [ STR: 5 | DEX: 7 | ARM: 1 | MOX: 16 ] [ COINS: 77 | HEALTH: 80 ]

ELF 2 [ STATUS: 活着的精灵 ] [ CLAN: 城市 | BEST FRIEND: HOBBIT 1 ] [ STR: 14 | DEX: 20 | ARM: 18 | MOX: 12 ] [ COINS: 1000 | HEALTH: 80 ]

主类部分:

        // HOBBIT 1 和 2
		System.out.println("\n让我们创建两个新的霍比特人!");
		System.out.println("正在创建两个新的霍比特人...");
		System.out.println("\n霍比特人 1 的名字:");
		String hobbitName = sc.nextLine();
		Hobbit hobbit1 = new Hobbit(hobbitName);

		System.out.println("霍比特人 2 的名字:");
		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 和 2
		System.out.println("\n让我们创建两个新的精灵!");
		System.out.println("正在创建两个新的精灵...");
		System.out.println("\n精灵 1 的名字:");
		String elfName1 = sc.nextLine();
		Elf elf1 = new Elf(elfName1);

		System.out.println("精灵 2 的名字:");
		String elfName2 = sc.nextLine();
		Elf elf2 = new Elf(elfName2, 14, 20, 18, 12, 1000, 80, "森林", hobbit1);
		System.out.println("\n" + elf1);
		System.out.println(elf2);

精灵类代码部分:

import java.util.Random;

public class Elf extends Humanoid {

    private final String clan;    
    private String bestFriend;   

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

        // 随机设置氏族
        Random random = new Random();
        if(random.nextBoolean()){
            clan = "森林";
        } else {
            clan = "城市";
        }
    }

    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;
    }
    
    // 仅适用于精灵 2 的行
    @Override
    public String toString() {
        return getName() +
                " [ STATUS: " + super.isAlive() +
                " 精灵 ] [ CLAN: " + clan +
                " | BEST FRIEND: " + getBestFriend() +
                " ]" + super.toString();
    }
}
英文:

I keep having a object come up as null for Elf 1 which is supposed to show BEST FRIEND: HOBBIT 2 but it only shows null. I tried to look for solutions but nothing helped me. Would anyone please help me figure out a solution? Thank you.

example 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 ]

main 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);

code in elf 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();
}
}```
</details>
# 答案1
**得分**: 2
你正在调用构造函数 `Elf(String)` 来创建 elf1;这个构造函数设置了族群和名字,仅此而已。没有任何东西会设置“最好的朋友”,所以它保持为空。
<details>
<summary>英文:</summary>
You’re calling the constructor `Elf(String)` to create elf1; this constructor sets the clan and the name and that’s all. Nothing ever sets the “best friend” so it remains null.
</details>

huangapple
  • 本文由 发表于 2020年7月27日 10:05:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63107730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定