java如何将我的方法从我的子类使用到我的主类中?
我试图从我的主类hangman中的子类hangmanwords调用这些方法。代码如下:
public class hangManWords {
//declares words
String [] words = { "Highschool", "Government", "Continents", "Professors", "Programmer", "Dealership", "Controller", "Motorcycle", "Lightsaber"};
public void randomizeWords(String [] words) {
//randomize the words
for (int i = words.length - 1; i > 0; i--) {
//generate a random index
int j = (int)(Math.random() * (i + 1));
//swaps list i with list j
String temp = words[i];
words[i] = words[j];
words[j] = temp;
}
}
public String getNextWord (String [] words) { //gets the next random word
for (int i = 0; i < words.length; i++) {
if (words[i] == null) {
continue;
}
String temp = words[i];
words[i] = null;
return temp;
}
return null;
}
}
以下是我的主要部分,我试图使用它:
randomizeWords(words); //randomly generates a word from the word list
//players first word
guessThisWord = getNextWord(words);
guessThisWord = hideWord(guessThisWord, originalWord);//hides the word with _
# 1 楼答案
Call a method of subclass in Java
# 2 楼答案
在基类中声明虚拟方法,在子类中实现它们。现在,如果对象是子类类型,那么在基类中声明的方法中对这些虚拟方法的所有调用都将调用子类方法
# 3 楼答案
你是说像这样吗
注释
您应该考虑启动^ {CD1>}大写的名称,如^ {< CD2>}/P>