作者aa955123 (古靈精怪槍)
看板java
標題[問題] class的一個小問題
時間Thu Mar 5 19:12:31 2009
剛剛在測試
打了這串程式碼
public class testcode
{
class test
{
double hight;
double width;
void area()
{
System.out.println("面積是:");
double ans = (hight*width);
System.out.println(ans);
}
}
public static void main( String args [])
{
test a=new test();
a.hight = 10;
a.width = 10;
a.area();
}
}
出現以下的錯誤
non-static variable this cannot be referenced from a static context
test a=new test();
^
我仔細的跟書上寫的對照了好幾次 就是不知道到底哪裡出錯@@"
請大家幫幫忙
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.42.213.144
推 carlcarl: class test() ? 03/05 19:20
→ aa955123:可是 書上在宣告class的時候後面並沒有加()耶 03/05 19:24
→ aa955123:我知道問題出在哪了!! 謝謝各位幫忙 XD 03/05 19:30
推 howard666:add "static" on class 03/05 20:31
推 chaostorm:應該是內隱(inner class) 03/05 20:36
→ chaostorm:改成testcode.test a = new testcode().new test(); 03/05 20:38
推 chaostorm:應該就OK了 ^^ 03/05 20:42
推 kucom:為什麼要加static? 有什麼意義呢? 03/05 20:43
推 chaostorm:public static void main 03/05 20:52
推 carlcarl:喔 看錯了XD 03/06 00:56