2014-12-03
13:57:48
1 /** 2 * @author nlee 3 * @version 1:09:38 PM Dec 3, 2014 4 */ 5 package test; 6 7 public class T extends T1 8 { 9 int b;10 11 public T(int s1)12 {13 super(s1);14 b = 111;15 }16 17 public void fun2()18 {19 System.out.33行被注釋的代碼
33 // System.out.println(t1.b);
若執(zhí)行則會報錯。
因為29行代碼
29 T1 t1 = t;
將子類對象實例賦給父類對象,t1和t指向是同一內(nèi)存塊,但是t1不能訪問子類的字段,因此t1不能訪問變量b。
新聞熱點
疑難解答