我需要一個數組是公有的(可以被類中的其他方法訪問),但是數組需要一個輸入值「T」來創建它。如何實例化需要用戶輸入的「全局」變量?如何在需要參數的情況下實例化新的公共Java類?
我的代碼如下:
public class PercolationStats {
**private double myarray[];**
public PercolationStats(int N, int T) {
**double myarray = new double[T];**
for (i=0;i<T;i++) {
Percolation percExperiment as new Percolation(N);
//do more stuff, make calls to percExperiment.publicmethods
myarray[i] = percExperiment.returnvalue;
}
}
public static void main(String[] args) {
int N = StdIn.readInt();
int T = StdIn.readInt();
PercolationStats percstats = new PercolationStats(N, T);
//do more stuff, including finding mean and stddev of myarray[]
StdOut.println(output);
}
在僞又如:
class PercolationStats {
Constructor(N, T) {
new Percolation(N) //x"T" times
}
Main {
new PercolationStats(N, T) //call constructor
}
}
class Percolation {
Constructor(N) {
**new WQF(N)** //another class that creates an array with size dependent on N
}
Main {
**make calls to WQF.publicmethods**
}
}
在第二個例子,在我看來,我需要有一流的WQF所做的新實例在Percolation的構造函數中,以接受參數N.但是,WQF不可用於Main方法的滲濾。 幫助!
。匆匆一瞥,人們會認爲你正在用'T'等語言泛泛而談。 –
你的第二個例子非常混亂。爲什麼你有兩個主要方法? –
'Percolation percExperiment as new Percolation(N)' - 這甚至是Java嗎? –