題目:
實現一個程式,程式中包含的人物按國籍劃分為:中國人,美國人,日本人,歐洲人這幾種人類具備很多相同的屬性(如身高,體重等)及一些特有的屬性(如姓名的構成結構不一樣等)這幾種人還具備一些相同的行為,但是實現方式各不一樣(如吃飯有用筷子有些用刀叉有些用手抓等)這幾種人對某種行為實現也可以有多種方式(如去上班可以走路,開車,騎單車等)
package com.humanactivity;
public class Person {
protected float height;// 身高
protected float weight;// 體重
String firstname;//姓
String middlename;//
String lastname;//名
/**
*
* @param height
* @param weight
*/
public Person(float height, float weight) {
this.height = height;
this.weight = weight;
}
/**
* 出行
*/
public void travelling(){
}
/**
*
*/
public void show() {
System.out.println("身高:\t" + this.height + "\t體重:\t" + this.weight);
}
public float getHeight() {
return height;
}
public float getWeight() {
return weight;
}
public void setHeight(float height) {
this.height = height;
}
public void setWeight(float weight) {
this.weight = weight;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
package com.humanactivity;
public class American extends Person implements IBehaviour {
public American(String lastname,String middlename,
String firstname,float height, float weight) {
super(height, weight);
//美國人名字構成
//last name=family name姓氏 middle name是中間名字,通常會是教名或為了紀念誰
//first name + middle name = given name名字
//for example: John William Croft
String name=firstname+"."+middlename+" "+lastname;
System.out.println("American:\t"+name);
// TODO Auto-generated constructor stub
}
// 實現介面方法
public void drink() {
// TODO Auto-generated method stub
System.out.println("美國人喜歡喝可口可樂...");
}
public void drive() {
// TODO Auto-generated method stub
System.out.println("美國人開車上班...");
}
public void eat() {
// TODO Auto-generated method stub
System.out.println("美國人吃飯用刀叉...");
}
public void onFoot() {
// TODO Auto-generated method stub
}
public void ride_a_bike() {
// TODO Auto-generated method stub
}
/**
* 顯示資訊
*/
public void show(){
super.show();
}
/**
* 出行方法
*/
public void travelling() {
//super.travelling();
this.drive();
}
}
////////////////////////////////////////////////////////////////////////////
package com.humanactivity;
public interface IBehaviour {
/**
* 介面實現不同是行為
*/
//吃
public void eat();
//喝
public void drink();
//步行
public void onFoot();
//開車
public void drive();
//騎單車
public void ride_a_bike();
}
////////////////////////////////////////////////////////////////
package com.humanactivity;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// John William Croft
American a = new American("Croft", "William", "John", 120, 45);
a.show();
a.eat();
a.drink();
a.travelling();
// 姓+名
Chinese c = new Chinese("王", "明", 170, 45);
c.show();
c.eat();
c.drink();
c.travelling();
// 日本人只有姓
Japanese j = new Japanese("佐藤", 160, 50);
j.show();
j.eat();
j.travelling();
// 日本人只有姓
European e = new European("Hammer", "Victor", 175, 50);
e.show();
e.eat();
e.travelling();
}
}