隨筆-19  評論-2  文章-1  trackbacks-0

           <java編程語言 >

           

          多太中的一種情況

          在類的多太中,域是由他引用的類型決定那個版本,而方法則是由對象的真實類決定的。這和接口中使用的一樣。

          測試代碼如下:

          class Father{

                 public String str="father";

                 public void fun(){

                        System.out.println("Father : " + str);

                 }

          }

          class ChildOne extends Father{

                 public String str="childone";

                 public void fun(){

                        System.out.println("ChildOne : " + str);

                 }

          }

          public class TestClassProMeth {

                 public static void main(String args[]){

                        ChildOne childone = new ChildOne();

                        Father father = childone;

                        father.fun();

                        childone.fun();

                        System.out.println(father.str + '\n' + childone.str);

                 }

          }

          結果:

          ChildOne : childone

          ChildOne : childone

          father

          childone

           

          接口:

          在接口中的聲明的域是是一個常量(public static final),他的方法不能夠有其他的修飾符,隱式為public,也不能夠被擁有定義實現符的修飾如:nativesynchronizedstrictfp也不能夠是靜態的,因為靜態方法不能夠抽象方法。

          繼承接口:如果接口聲明了一個和繼承來的常量名相同,則無論他的類型如何都將被隱藏,并且當他們的類型不一致時,編譯出錯。

           

          測試如下:

          interface Super{

                 public static final String name = "super";

                 void fun();

          }

          interface Entend extends Super{

                 String name = "entend";

                 String str = Super.name + '\n' + name;

                 void fun();

          }

          class Realize implements Entend{

                 public void fun(){

                        System.out.println(Super.name + '\n' + Entend.name + '\n' + str);

                 }

          }

          public class TestInterface {

                 static String name ="testinterface";

                 public static void main(String args[]){

                        Realize real = new Realize();

                        real.fun();

                        System.out.println(Realize.name + '\n' + ((Super)real).name);

                 }

          }

          結果:

          super

          entend

          super

          entend

          entend

          super

          posted on 2005-07-30 21:03 sky 閱讀(149) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 鄂尔多斯市| 彭州市| 沈阳市| 呼和浩特市| 临邑县| 绥滨县| 固原市| 九龙坡区| 呼伦贝尔市| 西充县| 安陆市| 武清区| 滁州市| 施秉县| 铁岭县| 林州市| 平潭县| 汕头市| 锦屏县| 玉林市| 宁明县| 米脂县| 灵武市| 襄樊市| 宁国市| 温宿县| 湘乡市| 三门县| 和硕县| 武汉市| 巨鹿县| 崇阳县| 手游| 婺源县| 横山县| 广西| 大丰市| 崇义县| 韶山市| 罗定市| 金阳县|