我们得到了 PercolationStats 类的这个规范:
public class PercolationStats {
public PercolationStats(int N, int T) // perform T independent computational experiments on an N-by-N grid
public double mean() // sample mean of percolation threshold
public double stddev() // sample standard deviation of percolation threshold
public double confidenceLo() // returns lower bound of the 95% confidence interval
public double confidenceHi() // returns upper bound of the 95% confidence interval
public static void main(String[] args) // test client, described below
}
为了实现 mean() 和 stddev(),我们必须使用一个特殊的库,它有一个名为 StdStats 的类:
public final class StdStats {
private StdStats() { }
/* All methods declared static. */
}
我试着写一些类似的东西
public mean() {
return StdStats.mean();
}
但我收到以下错误:
Cannot make a static reference to the non-static method mean() from the type PercolationStats
这大概是产生它的原因:
main() {
/* ... */
System.out.println("-- Summary --\n");
System.out.printf("mean\tstdev\t[lo\thi]\n\n");
System.out.printf("%1.3f\t%.3f\t%.3f\t%.3f", PercolationStats.mean(),
PercolationStats.stddev(), PercolationStats.confidenceLo(), PercolationStats.confidenceHi());
System.out.println("-- End --");
}
有没有办法在不改变规范的情况下摆脱这个错误?我相信我们应该能够制作 PercolationStats 对象。谢谢你的帮助!