類之謎:

          類之謎:

          一、令人混淆的構造器

          package com.huihui.text;

          ?

          public class Confusing {

          ?????? private Confusing(Object o){

          ????????????? System.out.print("object");

          ?????? }

          ?????? private Confusing(double[] a){

          ????????????? System.out.print("double");

          ?????? }

          ?????? public static void main(String args[]){

          ????????????? new Confusing(null);

          ?????? }

          }

          ?

          輸出; double

          ?

          解釋:精確性問題

          二、貍貓換太子

          ?

          package com.huihui.text;

          ?

          public class Counter {

          ?????? private static? int count;

          ?????? public? static void increament(){

          ????????????? count++;

          ?????? }

          ?????? /**

          ?????? ?* @return Returns the count.

          ?????? ?*/

          ?????? public static? int getCount() {

          ????????????? return count;

          ?????? }

          }

          ?

          package com.huihui.text;

          ?

          ?

          public class Dog extends Counter {

          ?????? public void woof(){

          ????????????? increament();

          ?????? }

          //???? public static void main(String args[]){

          //??????????? Dog d=new Dog();

          //??????????? d.increament();

          //???? }

          }

          ?

          package com.huihui.text;

          ?

          public class Cat extends Counter {

          ?????? public void meow() {

          ????????????? increament();

          ?????? }

          }

          ?

          package com.huihui.text;

          ?

          public class Test {

          ?????? public static void main(String args[]) {

          ????????????? Dog[] dogs = { new Dog(), new Dog() };

          ????????????? for (int i = 0; i < dogs.length; i++) {

          ???????????????????? dogs[i].woof();

          ????????????? }

          ????????????? Cat[] cats = { new Cat(), new Cat(), new Cat() };

          ????????????? for (int i = 0; i < cats.length; i++) {

          ???????????????????? cats[i].meow();

          ????????????? }

          ?????????????

          ????????????? System.out.print(Dog.getCount());

          ?????? }

          }

          ?

          輸出:5

          解釋:每一個靜態域在聲明他的類中及所有子類中共享一份單一的拷貝

          但你拿不準是,優先選擇組合而不是繼承

          posted on 2006-04-11 23:35 77 閱讀(138) 評論(0)  編輯  收藏


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


          網站導航:
           
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統計

          常用鏈接

          留言簿(12)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          API文檔

          java開發與研究

          にほん

          上海房產

          東京生活

          數據庫大全

          編程與開發

          美國開發生活

          走向管理

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 鸡西市| 尤溪县| 寿阳县| 竹溪县| 通州市| 昂仁县| 寻甸| 内江市| 勐海县| 嘉义市| 满城县| 松原市| 莱阳市| 建湖县| 遂昌县| 内丘县| 昌图县| 龙游县| 涞源县| 甘肃省| 汶川县| 长兴县| 抚顺县| 西和县| 富源县| 诸暨市| 奇台县| 霍邱县| 丹巴县| 鸡西市| 金川县| 景宁| 井冈山市| 久治县| 房山区| 沛县| 温州市| 涡阳县| 泰顺县| 浮梁县| 正镶白旗|