为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

一只甜甜圈 2023-09-27 16:55:57
为什么我的代码将 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' 有 2330000 人使用,主要分布在中美洲。该语言遵循词序:动词-宾语-主语 有趣的事实:Ki'che' 是一种作格语言。实际是:null 主要在中美洲有 0 人使用。该语言遵循词序:动词-宾语-主语 有趣的事实:null 是一种作格语言。
查看完整描述

1 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

在Mayan你有领域


protected String name;

protected int numSpeakers;

删除这些将解决您的问题。发生此问题的原因是因为当您定义上面的两行时,您隐藏了这两个字段,Language并且您必须从Languagelike super.name、super.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.");

    }

}

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


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

    }

}


查看完整回答
反对 回复 2023-09-27
  • 1 回答
  • 0 关注
  • 83 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信