1
public class Sentencegenerator {
private String[] subjects;
private String[] verbs;
private String[] directObjects;
public Sentencegenerator(){
subjects = {"Cat", "Dog", "Joe", "Teacher", "Policeman", "Doctor", "Dick"};
verbs = {"eats", "reads", "cums", "farts", "poops", "stabs", "cries"};
directObjects = {"book", "sticker", "fish", "man", "chiwawa", "marker", "cheese"};
}
public Sentencegenerator(String[] mySubjects, String[] myVerbs, String[] myDirectObjects){
subjects = mySubjects;
verbs = myVerbs;
directObjects = myDirectObjects;
}
我知道你只能在初始化一個數組時創建數組常量,但是有沒有辦法讓一個數組實例變量,然後使一個具有自己的常量的構造函數?如何將數組實例變量設置爲特定的一組數據?
我不明白。構造函數擁有自己的常量是什麼意思? –
例如,在主題中的數組常量是Cat,Dog,Joe,Teacher,Policeman,Doctor和Dick。我得到一個編譯器錯誤,因爲你創建數組後不能創建數組常量。我想知道一種方法,我仍然可以使用這些常量創建構造函數,但是我只是在構造函數中創建變量而猶豫不決,因爲我不確定在Java約定中是否允許這樣做。但是,如果這是唯一的方法,那就這樣吧。 – dirtysocks45