seaairland

           

          類之謎

          類之謎:

          一、令人混淆的構造器

          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-07 16:34 chenhui 閱讀(186) 評論(0)  編輯  收藏 所屬分類: java基礎

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          介紹 IOC

          友情鏈接

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 兴义市| 城市| 昆山市| 泾源县| 北票市| 逊克县| 师宗县| 砚山县| 保定市| 河南省| 拜城县| 舟山市| 梧州市| 金阳县| 宁河县| 灵丘县| 孙吴县| 阿荣旗| 龙泉市| 浑源县| 西城区| 额敏县| 防城港市| 长顺县| 星子县| 城步| 灵寿县| 中方县| 镇江市| 五大连池市| 来凤县| 吉林市| 施甸县| 凌海市| 镇坪县| 家居| 固始县| 井研县| 奎屯市| 玉田县| 建宁县|