我在尝试创建汉字类的实例时遇到问题。它应该有2个静态内部类。每个内部类应包含两个字符串。

当我实例化该类时,它将两个静态内部类返回为null。

完整的课程如下。我的构造函数正确吗?如果我没有内部类及其静态字符串,则会抛出错误。

Kanji kanji = new Kanji("aaa","bbb","ccc","ddd","eee","fff" );


我的课

public class Kanji {

public String character;
public String meaning;
public Onyomi onyomi;
public Kunyomi kunyomi;

public static class Onyomi {
    public static String romaji;
    public static String katakana;
    public String getRomaji() {
        return romaji;
    }

    public void setRomaji(String romaji) {
        this.romaji = romaji;
    }

    public String getKatakana() {
        return katakana;
    }

    public void setKatakana(String katakana) {
        this.katakana = katakana;
    }

}
public static class Kunyomi {
    public static String romaji;
    public static String hiragana;

    public String getRomaji() {
        return romaji;
    }

    public void setRomaji(String romaji) {
        this.romaji = romaji;
    }

    public String getHiragana() {
        return hiragana;
    }

    public void setHiragana(String hiragana) {
        this.hiragana = hiragana;
    }
}

public Kanji(String character_, String meaning_, String oR_, String oK_, String kR_, String kH_) {
    this.character = character_;
    this.meaning = meaning_;
    this.onyomi.romaji = oR_;
    this.onyomi.katakana = oK_;
    this.kunyomi.romaji = kR_;
    this.kunyomi.hiragana = kH_;
}

public String getCharacter() {
    return character;
}

public void setCharacter(String character) {
    this.character = character;
}

public String getMeaning() {
    return meaning;
}

public void setMeaning(String meaning) {
    this.meaning = meaning;
}

public Onyomi getOnyomi() {
    return onyomi;
}

public void setOnyomi(Onyomi onyomi) {
    this.onyomi = onyomi;
}

public Kunyomi getKunyomi() {
    return kunyomi;
}

public void setKunyomi(Kunyomi kunyomi) {
    this.kunyomi = kunyomi;
}
}

最佳答案

我认为这段代码有2个问题。

第一个(导致onyomikunyomi字段为null的事实)是,在构造函数代码中,您没有初始化这些变量,因此,不,构造函数不正确

您需要在构造函数中添加的是以下两行:

this.onyomi = new Onyomi();
this.kunyomi = new Kunyomi();


在设置onyomikunyomi字段之前,如下所示:

public Kanji(String character_, String meaning_, String oR_, String oK_, String kR_, String kH_) {
    this.character = character_;
    this.meaning = meaning_;
    this.onyomi = new Onyomi();
    this.kunyomi = new Kunyomi();
    this.onyomi.romaji = oR_;
    this.onyomi.katakana = oK_;
    this.kunyomi.romaji = kR_;
    this.kunyomi.hiragana = kH_;
}


此测试代码出现第二个问题:

Kanji ima = new Kanji("今","now","ima_on","イマ","ima_kun","いま" );
    System.out.println(ima);
    Kanji kuruma = new Kanji("車", "car", "kuruma_on", "クルマ", "kuruma_kun", "くるま");
    System.out.println(kuruma);
    System.out.println(ima);


我添加了一个toString函数,就像这样的汉字类:

public String toString() {
    return "Character="+character+" - Meaning="+meaning + "\nKunyomi=" + this.kunyomi.getRomaji() + "/" + this.kunyomi.getHiragana() + " - Onyomi="+onyomi.getRomaji()+"/"+onyomi.getKatakana();
}


它产生了以下输出:

Character=今 - Meaning=now
Kunyomi=ima_kun/いま - Onyomi=ima_on/イマ
Character=車 - Meaning=car
Kunyomi=kuruma_kun/くるま - Onyomi=kuruma_on/クルマ
Character=今 - Meaning=now
Kunyomi=kuruma_kun/くるま - Onyomi=kuruma_on/クルマ


如您所见,创建包含“汽车”汉字信息的日文对象也更改了包含“现在”汉字信息的日文对象的toString结果

这里的问题是Onyomi和Kunyomi类中的字段是静态的,这意味着在整个应用程序中这些字段将只有一个副本(或者每个Onyomi / Kunyomi类加载一个副本,我不确定),而不是为每个Onyomi / Kunyomi对象创建一个副本。

这意味着,每次创建新的汉字对象时,汉字构造函数都会删除以前的Kunyomi romaji和平假名字段,以及以前的Onyomi romaji和片假名字段。

因此,您可能想要删除字段的静态修饰符,如下所示:

public static class Onyomi {
    public String romaji;
    public String katakana;
    public String getRomaji() {
        return romaji;
    }

    public void setRomaji(String romaji) {
        this.romaji = romaji;
    }
    public String getKatakana() {
        return katakana;
    }
    public void setKatakana(String katakana) {
        this.katakana = katakana;
    }

}
public static class Kunyomi {
    public String romaji;
    public String hiragana;

    public String getRomaji() {
        return romaji;
    }

    public void setRomaji(String romaji) {
        this.romaji = romaji;
    }

    public String getHiragana() {
        return hiragana;
    }

    public void setHiragana(String hiragana) {
        this.hiragana = hiragana;
    }
}


这样,先前的测试代码将输出以下内容:

Character=今 - Meaning=now
Kunyomi=ima_kun/いま - Onyomi=ima_on/イマ
Character=車 - Meaning=car
Kunyomi=kuruma_kun/くるま - Onyomi=kuruma_on/クルマ
Character=今 - Meaning=now
Kunyomi=ima_kun/いま - Onyomi=ima_on/イマ


此外,汉字会自行初始化Kunyomi / Onyomi创建的对象的字段。这样做应该是Kunyomi / Onyomi构造函数的工作:

在Onyomi类代码中,添加以下构造函数:

    public Onyomi() {
        romaji = null;
        katakana = null;
    }

    public Onyomi(String romaji, String katakana) {
        this.romaji = romaji;
        this.katakana = katakana;
    }


在Kunyomi类中,添加以下构造函数:

    public Kunyomi() {
        romaji = null;
        hiragana = null;
    }

    public Kunyomi(String romaji, String hiragana) {
        this.romaji = romaji;
        this.hiragana = hiragana;
    }


在日文汉字构造函数中,您将可以像以前那样或那样进行:

public Kanji(String character_, String meaning_, String oR_, String oK_, String kR_, String kH_) {
    this.character = character_;
    this.meaning = meaning_;
    this.onyomi = new Onyomi(oR_, oK_);
    this.kunyomi = new Kunyomi(kR_, kH_);
}

08-04 22:17