看板 java 關於我們 聯絡資訊
剛剛在測試 打了這串程式碼 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
bala045:http://0rz.tw/fpHWr 03/05 23:59
carlcarl:喔 看錯了XD 03/06 00:56