返回列表 發帖
  1. public class Ch01 {

  2.         public static void main(String[] args) {
  3.                 Human h=new Human("湯尼", 35, 70);
  4.                 h.showProfile();
  5.                 h.eat(0.85);
  6.                 h.showProfile();
  7.                 h.swim(1500);
  8.                 h.sing("歌");
  9.                 h.takeCare();
  10.                 h.fly();
  11.         }

  12. }
  13. abstract class Animal
  14. {
  15.         int age;
  16.         double w;
  17.         Animal(int a,double w)
  18.         {
  19.                 age=a;
  20.                 this.w=w;
  21.         }
  22.         abstract void eat(double x);
  23.         abstract void showProfile();
  24. }
  25. interface Swimmer
  26. {
  27.         String LEVEL="專業級";
  28.         void swim(double x);
  29. }
  30. interface Singer
  31. {
  32.         String LEVEL="專業級";
  33.         void sing(String x);
  34. }
  35. interface Father
  36. {
  37.         String LEVEL="新手級";
  38.         void takeCare();
  39. }
  40. interface Genius extends Swimmer, Singer,Father
  41. {
  42.         String LEVEL="大神級";
  43.         void fly();
  44. }
  45. class Human extends Animal implements Genius
  46. {
  47.         String name;
  48.         Human(String n,int a,double w)
  49.         {
  50.                 super(a,w);
  51.                 name=n;
  52.         }
  53.         void showProfile()
  54.         {
  55.                 System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  56.         }
  57.         void eat(double x)
  58.         {
  59.                  System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
  60.                  w+=x;
  61.         }
  62.         @Override
  63.         public void swim(double x) {
  64.                 System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  65.         }
  66.         @Override
  67.         public void sing(String x) {
  68.                 System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
  69.         }
  70.         @Override
  71.         public void takeCare() {
  72.                 System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");       
  73.         }
  74.         @Override
  75.         public void fly() {
  76.                 System.out.println(name+"以"+Genius.LEVEL+"水準,快速揮動兩片扇子,飛了起來!");        
  77.         }
  78. }
複製代碼

TOP

返回列表