話說,人類是萬物之靈,所以人類也是物件的一種。然而要具體形容一個人,除了外觀,當然還有道德、職業、學經歷、聲望這些因素等等…這些要素,在還沒有能夠具體形容這
些要素前,我們能做的,就是暫時將人類塑造為一個簡陋的抽象類別。
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 <> 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 comments:
Post a Comment
有什麼想說的嗎?