[問題] class的一個小問題

看板java作者 (古靈精怪槍)時間17年前 (2009/03/05 19:12), 編輯推噓8(803)
留言11則, 6人參與, 最新討論串1/1
剛剛在測試 打了這串程式碼 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

03/05 19:20, , 1F
class test() ?
03/05 19:20, 1F

03/05 19:24, , 2F
可是 書上在宣告class的時候後面並沒有加()耶
03/05 19:24, 2F

03/05 19:30, , 3F
我知道問題出在哪了!! 謝謝各位幫忙 XD
03/05 19:30, 3F

03/05 20:31, , 4F
add "static" on class
03/05 20:31, 4F

03/05 20:36, , 5F
應該是內隱(inner class)
03/05 20:36, 5F

03/05 20:38, , 6F
改成testcode.test a = new testcode().new test();
03/05 20:38, 6F

03/05 20:42, , 7F
應該就OK了 ^^
03/05 20:42, 7F

03/05 20:43, , 8F
為什麼要加static? 有什麼意義呢?
03/05 20:43, 8F

03/05 20:52, , 9F
public static void main
03/05 20:52, 9F

03/05 23:59, , 10F
03/05 23:59, 10F

03/06 00:56, , 11F
喔 看錯了XD
03/06 00:56, 11F
文章代碼(AID): #19hxEZH2 (java)