精華區beta java 關於我們 聯絡資訊
※ 引述《www@bbs.ee.ntu.edu.tw (web)》之銘言: : 人的特色只有好人與坏人嗎? 這麼富有哲學性的問題,既然在Java板出現了,就一定要認真地用Java的方式來回答。 話說,人類是萬物之靈,所以人類也是物件的一種。然而要具體形容一個人,除了外觀, 當然還有道德、職業、學經歷、聲望這些因素等等…這些要素,在還沒有能夠具體形容這 些要素前,我們能做的,就是暫時將人化做一個簡陋的抽象類別。 package universe.earth; public abstract class Human { protected boolean isNormal; protected boolean isGoodGuy; protected String sex; protected double weight; protected double height; protected double BMI; public Human(){} public abstract boolean IsNormal(boolean eyebrow); public abstract boolean IsGoodGuy(String skill); public abstract void setBMI(double weight, double height); } 基本上,人類又可分為帶把與沒帶把的!皆下來我們就要實做這個帶把的男人類別,他繼 承自Human這個抽象類別。 package universe.earth; public class Male extends Human{ // Of course, this a male class public Male(){ sex = "male"; this.isNormal = true; this.isGoodGuy = false; // 人心險惡,先認為不是好人吧! } // Is his look normal? public boolean IsNormal(boolean eyebrow){ if(eyebrow){ this.isNormal = true; }else{ this.isNormal = false; } return isNormal; } // Is he a good guy? public boolean IsGoodGuy(String skill){ if(skill == null || skill.equalsIgnoreCase("java")){ this.isGoodGuy = true; }else{ this.isGoodGuy = false; // Are you a C# user?; } return isGoodGuy; } // Check his body is strong, weak, or normal. public void setBMI(double weight, double height){ this.weight = weight; this.height = height; BMI = 10000*weight/Math.pow(height, 2); } // overridden toString() public String toString(){ String description = "He is a " + sex +".\n"; if(isNormal && isGoodGuy && BMI >0){ description += "Good! You should make friend with him."; }else if(!isNormal && isGoodGuy && BMI >0){ description += "He might be a good friend, but don't laught at his eyebow."; }else if(!isNormal && !isGoodGuy && BMI >0){ description += "Please, he's just with no eyebrow!"; }else if(BMI < 18.5 && BMI > 0){ description += "You might win at this fight!"; }else if(BMI > 24.9){ description += "Run! Frost! Run!"; }else { description += "It's a even fight!"; } return description+"\n"; } } 好!我們做到這,我們可以基於一個人有沒有眉毛、專長、以及BMI決定這個人好不好相 處。所以我們開始將這個male類別實例化。 package universe.earth; public class JudgeHuman { public final static void main(String[] args){ Male thisGuy = new Male(); // 路上兩人相見,不熟悉! System.out.println("You haven't analyze who he is..."); System.out.println(thisGuy.toString()); // 然後你推測他的身高體重,算出他的BMI... thisGuy.setBMI(100,183); System.out.println("After you know his BMI..."); System.out.println(thisGuy.toString()); // 然後你又看了一下他的臉,發現他沒有眉毛! thisGuy.IsNormal(false); System.out.println("After you analyze his face..."); System.out.println(thisGuy.toString()); // 最後他告訴你,他原來是個java程式設計師! thisGuy.IsGoodGuy("java"); System.out.println("After you know his skills..."); System.out.println(thisGuy.toString()); } } 將這個Java application執行後的結果如下: You haven't analyze who he is... He is a male. It's a even fight! After you know his BMI... He is a male. Run! Frost! Run! After you analyze his face... He is a male. Please, he's just with no eyebrow! After you know his skills... He is a male. He might be a good friend, but don't laught at his eyebow. 所以,結論就是... 人的特色當然不是只有好人或壞人囉! (好吧!我承認我來騙稿費的...) -- No Dying Skills, ● - Dame! New tech... but Lazy Users. .\) http://www.wretch.cc/blog/hougzou ___________ ﹒ ︵ √\ ___________________ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.104.1.128 > -------------------------------------------------------------------------- < 發信人: qrtt1.bbs@bbs.sayya.org (foolish), 看板: java 標 題: Re: 人的特色只有好人與坏人嗎? 發信站: SayYA 資訊站 (Sun Sep 3 18:55:53 2006) 轉信站: ptt!ctu-reader!news.nctu!SayYa > 發信人: hougzou.bbs@ptt.cc (好像冷氣吹太多了), 看板: java > 標 題: Re: 人的特色只有好人與坏人嗎? > 發信站: 批踢踢實業 (Wed Aug 30 14:16:50 2006) > 轉信站: SayYa!ctu-reader!news.nctu!ptt > Origin: sally.csie.ntu.edu.tw > ※ 引述《www@bbs.ee.ntu.edu.tw (web)》之銘言: > : 人的特色只有好人與坏人嗎? > 這麼富有哲學性的問題,既然在Java板出現了,就一定要認真地用Java的方式來回答。 > 話說,人類是萬物之靈,所以人類也是物件的一種。然而要具體形容一個人,除了外觀, > 當然還有道德、職業、學經歷、聲望這些因素等等…這些要素,在還沒有能夠具體形容這 > 些要素前,我們能做的,就是暫時將人化做一個簡陋的抽象類別。 > package universe.earth; > public abstract class Human { > protected boolean isNormal; > protected boolean isGoodGuy; > protected String sex; > protected double weight; > protected double height; > protected double BMI; > public Human(){} > public abstract boolean IsNormal(boolean eyebrow); > public abstract boolean IsGoodGuy(String skill); > public abstract void setBMI(double weight, double height); > } > 基本上,人類又可分為帶把與沒帶把的!皆下來我們就要實做這個帶把的男人類別,他繼 > 承自Human這個抽象類別。 呵,自己來隨意改寫看看好了。一個"人" "類"應該要有什麼樣的內容呢?如果原po 所講的,實在太多種實作取向了。若是早個一二年看到這文章,肯定只是覺得很幽 默,與版友一同歡樂觀賞。不過最進有稍為長了一點知識,這例子讓我思考了很久 我們真的要這樣做嗎? 當然這有趣的例子不是原PO用來展示設計用的,所以就讓自 我延伸練習。是否可以改用比較有彈性的作法? 以design pattern的守則之一,變動的部分要被封裝起來。所謂的變動可能是一種 主觀的認定,在只認可男與女二種性別的情況下,性別是不會有什麼變動的。所以 ,這一部分通常關於專案的特性而有所差別。而"人"是好人或壞人,則是屬性一個 人的特質,或個性。但一個人的特質與個性往往難以估計有多少描述方式與分類, 暫且認為其為變動的部分,應該被封裝起來。 要如何來實現封裝這一個動作,回想起剛學習java還不懂封起來要做什麼,除了資 料成了黑盒子內的寶物之外,我們只看得到封裝後所提供的介面。他到底有什麼好 呢? 基本上的好處是擴充性,因為不必被侷限於base class。只要多加個implements 就多了點東西,base class是牽一髮而動全身的。一但把什麼都塞到base class後 ,那只要需求改變了,子類別全都要跟著改變。 俺用極端一點的風格,寫下了這個空虛的人類: ====================================================================== # public class HumanBeing { } 人活著不能如此空虛,回頭看看原先的實作,我們需要一點基本資料。 但是基本資料所蒐集的內容也不盡相同,在這不確定的年代, 我們要為適應變動而設想。將之獨立成"基本資料"介面 ====================================================================== # public interface GeneralInformation { public void setName(String name); public String getName(); public void setHeight(double height); public double getHeight(); public void setWeight(double weight); public double getWeight(); } public class HumanBeing implements GeneralInformation{ ................. } 一個有基本資料的人,我們可以為了這人的用途不同附加不同的介面。 例如為作家提供經歷、著作等資料的界面。為個性,提供個性集合的介面; 為個人特質、狀態提供介面 ====================================================================== # import java.util.Set; public interface Traits { public void setTraits(Set<Trait> traits); public Set<Trait> getTraits(); public void addTrait(Trait t); } public class Trait { String description; } public class HumanBeing implements GeneralInformation, Traits{ ............... } (再加一個諮商記錄幾乎就是諮商師用的application了..orz) ============================================================== 由這樣的想法,為一個基本的人提供不同的介面。而可用於不同的用途 ,比起繼承的方式要容易維護。 此外,由這一篇內容也聯想到,人的特質、好人、壞人等"形容詞"修飾 也適合用裝飾者模式來作。好人、壞人通通都是人,符合裝飾著的基本 條件,裝飾者的型態和被裝飾者是同一個型態 (實作上利用繼承的方式 達到同型態的目的,但把加料的部分塞在method以override的手法達成 )。 class 神奇寶貝 {} interface 基本資料 {學名、生長地區、基本型態} interface 攻擊系統 {一般攻擊、必殺技、攻擊加乘} class 神奇寶貝原型 extends 神奇寶貝 implements 基本資料, 攻擊系統{} class 皮卡丘 extends 神奇寶貝原型{ 學名: 皮卡丘 生長地區: foo place 基本型態: foo type 一般攻擊: foo action , 加乘 * 1.2 必殺技: killer action, 加乘 * 12 (傳說中的十萬伏特電擊!?) 攻擊加乘: 1 * 攻擊型態加乘 + bouns } 用裝飾者修飾皮卡丘 class 爆走皮卡丘 extends 皮卡丘{ 皮卡丘 p; public 爆走皮卡丘(皮卡丘 p) 一般攻擊: 加乘* p.攻擊加乘 * 10; 必殺技: 加乘* p.攻擊加乘 * 10^1.5; } class 好人皮卡丘 extends 皮卡丘{ 皮卡丘 p; public 爆走皮卡丘(皮卡丘 p) 一般攻擊: 加乘* p.攻擊加乘 * 0.1; 必殺技: 加乘* p.攻擊加乘 * 0.9; } orz..好唄。真是一整個胡言亂語。 -- ※ Origin: SayYA 資訊站 <bbs.sayya.org> ◆ From: 163.26.34.20