繼承,介面,多態練習java

來源:互聯網
上載者:User

題目:

實現一個程式,程式中包含的人物按國籍劃分為:中國人,美國人,日本人,歐洲人這幾種人類具備很多相同的屬性(如身高,體重等)及一些特有的屬性(如姓名的構成結構不一樣等)這幾種人還具備一些相同的行為,但是實現方式各不一樣(如吃飯有用筷子有些用刀叉有些用手抓等)這幾種人對某種行為實現也可以有多種方式(如去上班可以走路,開車,騎單車等)

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();

 }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.