我正在閱讀Just Java 2書,但顯然缺少一些基本的東西。這是兩個單獨的項目。我爲第二個生成了JAR,並將其添加到第一個構建路徑。打印正確的區域,但編譯器會生成這些警告。這些如何解決?如何解決「靜態方法___應該以靜態方式訪問」警告
// -----------------------------------------------------------
// Testing.java
// -----------------------------------------------------------
public class Testing {
public static void main(String[] args) {
RectangleDFC r = new RectangleDFC(3, 4);
System.out.println(r.Area());
// WARNING: The static method Area() from the type RectangleDFC
// should be accessed in a static way
r.SetSides (10, 10);
// WARNING: The static method SetSides(int, int) from the type
// RectangleDFC should be accessed in a static way
System.out.println(r.Area());
// WARNING: The static method Area() from the type RectangleDFC
// should be accessed in a static way
}
}
// -----------------------------------------------------------
// RectangleDFC.java
// -----------------------------------------------------------
public class RectangleDFC {
int side1;
int side2;
RectangleDFC(int s1, int s2) {
SetSides(s1, s2);
}
public void SetSides(int s1, int s2) {
side1 = s1;
side2 = s2;
}
public int Area() {
return side1 * side2;
}
}
+1用於搗毀這本書。我的同事喜歡「Effective Java」。 – 2010-07-07 20:29:19
「有效Java」不適合初學者。嘗試「用Java思考」。我確信有一個問題...... – 2010-07-07 22:10:05
「把它放在垃圾箱裏,拿到一本新書。」
tony9099
2013-08-30 14:55:41