为什么 child class 方法打印 null ?我究竟做错了什么?

Why child class method is printing null ? What am i doing wrong?

为什么我的代码将 null 作为输出值而不是传递的参数的任何提示。请指导我完成代码。

Parent Class:

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();
  }
}

Child Class:

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' 主要在中美洲有 2330000 人使用。 语言遵循词序:verb-object-subject 有趣的事实:Ki'che' 是一种作格语言。

实际是: null 主要在中美洲有 0 人使用。 语言遵循词序:verb-object-subject 有趣的事实:null 是一种作格语言。

Mayan 中你有字段

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 所指出的,可以限制访问修饰符。如果您在程序的单个包中工作,您可能会得到类似

的结果

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