1 回答

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊
在Mayan你有領(lǐng)域
protected String name;
protected int numSpeakers;
刪除這些將解決您的問(wèn)題。發(fā)生此問(wèn)題的原因是因?yàn)楫?dāng)您定義上面的兩行時(shí),您隱藏了這兩個(gè)字段,Language并且您必須從Languagelike super.name、super.numSpeakers等訪(fǎng)問(wèn)這些字段...
您可能正在追求類(lèi)似以下的內(nèi)容。
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.");
}
}
正如加文指出的,訪(fǎng)問(wèn)修飾符可以受到限制。如果您在程序的單個(gè)包中工作,您最終可能會(huì)得到類(lèi)似的結(jié)果
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.");
}
}
添加回答
舉報(bào)