返回列表 發帖
  1. public class CH01{
  2.         public static void main(String[] args) {
  3.                 Human h1=new Human("湯尼",35,70);
  4.                 h1.showProfile();
  5.                 h1.eat(0.85);
  6.                 h1.showProfile();
  7.                 h1.swim(100);
  8.                 h1.sing("蝴蝶");
  9.                 h1.takeCare();
  10.         }
  11. }

  12. abstract class Animal
  13. {
  14.     int age;
  15.     double w;
  16.     Animal(int a,double w)
  17.     {
  18.             age=a;
  19.             this.w=w;
  20.     }
  21.     abstract void eat(double x);
  22.     abstract void showProfile();
  23. }

  24. interface Swimmer
  25. {
  26.     String LEVEL="專業級";
  27.     void swim(double x);
  28. }

  29. interface Singer
  30. {
  31.     String LEVEL="專業級";
  32.     void sing(String x);
  33. }

  34. interface Father
  35. {
  36.     String LEVEL="新手級";
  37.     void takeCare();
  38. }

  39. class Human extends Animal implements Swimmer, Singer, Father
  40. {
  41.     String name;
  42.     Human(String n,int a,double w)
  43.     {
  44.             super(a,w);
  45.             name=n;
  46.     }
  47.     void showProfile()
  48.     {
  49.             System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  50.     }
  51.     void eat(double x)
  52.     {
  53.         System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
  54.         w+=x;
  55.     }
  56.     public void swim(double x)
  57.     {
  58.             System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  59.     }
  60.     public void sing(String x)
  61.     {
  62.             System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
  63.     }
  64.     public void takeCare()
  65.     {
  66.             System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
  67.     }   
  68. }
複製代碼

TOP

返回列表