gpt4 book ai didi

java - 为什么子类方法打印 null ?我究竟做错了什么?

转载 作者:行者123 更新时间:2023-12-02 09:27:19 26 4
gpt4 key购买 nike

为什么我的代码将 null 作为输出值而不是传递的参数?

父类:

class Language{
protected String name;
protected int numSpeakers;
protected String regionsSpoken;
protected String wordOrder;

public Language(String getName, int getNumSpeakers, String getRegionsSpoken, String getWordOrder){
this.name = getName;
this.numSpeakers = getNumSpeakers;
this.regionsSpoken = getRegionsSpoken;
this.wordOrder = getWordOrder;
}

public void getInfo(){
System.out.println(name+ " is spoken by "+numSpeakers+" people mainly in "+regionsSpoken);
System.out.println("The language follows the word order: "+wordOrder);
}

public static void main(String[] args){
Mayan mayanLanguage = new Mayan("Ki'che'",30000);
mayanLanguage.getInfo();
}
}

子类:

class Mayan extends Language {
protected String name;
protected int numSpeakers;

Mayan(String languageName,int speakers ){
super(languageName,speakers,"Central America","verb-object-subject");
}

@Override
public void getInfo() {
System.out.println(name+" is spoken by "+numSpeakers+" people mainly in Central America.");
System.out.println("The language follows the word order: verb-object-subject");
System.out.println("Fun fact: "+name+" is an ergative language.");
}
}

我已经研究了代码并尝试通过更改来解决它,但似乎没有任何效果,我陷入了代码中没有看到的错误是什么。

预期是:

Ki'che' is spoken by 2330000 people mainly in Central America.The language follows the word order: verb-object-subjectFun fact: Ki'che' is an ergative language.

实际是:

null is spoken by 0 people mainly in Central America.The language follows the word order: verb-object-subjectFun fact: null is an ergative language.

最佳答案

玛雅中,你有田野

protected String name;
protected int numSpeakers;

删除这些将解决您的问题。发生此问题的原因是,当您定义上面的两行时,您从 Language 隐藏了这两个字段,并且您必须从 Language 访问这些字段,例如 super.namesuper.numSpeakers等...

您可能想要的内容如下所示。

public class Mayan extends Language {
private static final String REGION = "Central America";
private static final String WORD_ORDER = "verb-object-subject";

public Mayan(String languageName, int speakers) {
super(languageName, speakers, REGION, WORD_ORDER);
}

@Override
public void getInfo() {
super.getInfo();
System.out.println("Fun fact: " + name + " is an ergative language.");
}
}
<小时/>

正如 Gavin 指出的,访问修饰符是可以受到限制的。如果您在程序的单个包中工作,您最终可能会得到类似的东西

语言.java

class Language {
String name;
private int numSpeakers;
private String regionsSpoken;
private String wordOrder;

Language(String getName, int getNumSpeakers, String getRegionsSpoken, String getWordOrder) {
this.name = getName;
this.numSpeakers = getNumSpeakers;
this.regionsSpoken = getRegionsSpoken;
this.wordOrder = getWordOrder;
}

void getInfo() {
System.out.println(name + " is spoken by " + numSpeakers + " people mainly in " + regionsSpoken);
System.out.println("The language follows the word order: " + wordOrder);
}

public static void main(String[] args) {
Mayan mayanLanguage = new Mayan("Ki'che'",30000);
mayanLanguage.getInfo();
}
}

Mayan.java

class Mayan extends Language {
private static final String REGION = "Central America";
private static final String WORD_ORDER = "verb-object-subject";

Mayan(String languageName, int speakers) {
super(languageName, speakers, REGION, WORD_ORDER);
}

@Override
void getInfo() {
super.getInfo();
System.out.println("Fun fact: " + name + " is an ergative language.");
}
}

关于java - 为什么子类方法打印 null ?我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58255295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com