你可能想聲明領域,並獲得在構造函數中的參數值,參數保存到字段:
public static class TCP_Ping implements Runnable {
// these are the fields:
private final int a;
private final String b;
// this is the constructor, that takes parameters
public TCP_Ping(final int a, final String b) {
// here you save the parameters to the fields
this.a = a;
this.b = b;
}
// and here (or in any other method you create) you can use the fields:
@Override public void run() {
System.out.println("a: " + a);
System.out.println("b: " + b);
}
}
然後你就可以像這樣創建類的實例:
TCP_Ping ping = new TCP_Ping(5, "www.google.com");
我建議你[Java中開始](http://docs.oracle.com/javase/tutorial/java/javaOO/index.html)。 關於您的問題:http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html – blint 2013-04-06 01:48:03
類不是「靜態」。方法,實例變量和初始化塊可以是'static'。然而,人們濫用這一點。 – 2013-04-06 02:30:40