[語法] java繼承問題

看板java作者 (bruce)時間8年前 (2017/03/21 15:05), 編輯推噓0(006)
留言6則, 4人參與, 最新討論串1/1
想問大家 這個code哪裡錯了 public class a1 { public static void main(String args[]) { double a; area C1 =new area(); a=C1.area1(5, 6); } class area{ public double area1(double a, double b){ return a+b; } } } 這個area C1 =new area(); 這裡 Eclipse一直出現錯誤 這不就是呼叫area建構子 為什一直有錯 No enclosing instance of type a1 is accessible. Must qualify the allocation with an enclosing instance of type a1 (e.g. x.new A() where x is an instance of a1). 這個是什麼意思 可以請高手幫忙解答嗎 小弟是java初學者 -- 木瓜種子居然也能種成盆栽 夠神奇吧 http://www.newsancai.com/b5/health/9-pedia/75624-.html -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.230.250.239 ※ 文章網址: https://www.ptt.cc/bbs/java/M.1490079946.A.169.html

03/21 15:32, , 1F
area是a1的inner class(也就是area的物件一定要關聯到一個
03/21 15:32, 1F

03/21 15:34, , 2F
a1物件),所以要先new一個a1的物件,才能用它new area
03/21 15:34, 2F

03/21 18:57, , 3F
要不把area 搬到外面,要不改成static nested class
03/21 18:57, 3F

03/22 21:33, , 4F
因為是inner class的關係無誤,另外class習慣上大寫開頭
03/22 21:33, 4F

03/22 21:34, , 5F
還有就是繼承應該是在講另一個故事...
03/22 21:34, 5F

03/23 14:19, , 6F
static class area 就會過了吧
03/23 14:19, 6F
文章代碼(AID): #1OqD3A5f (java)
文章代碼(AID): #1OqD3A5f (java)