返回列表 發帖
  1. public class Ch73 {
  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(1500);
  8.                 h1.sing("新不了情");
  9.                 h1.takeCare();
  10.                 h1.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.     }
  23.     abstract void eat(double x);
  24.     abstract void showProfile();
  25. }

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

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

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

  41. interface Genius extends Swimmer, Singer, Father
  42. {
  43.     String LEVEL="大神級";      
  44.     void fly();
  45. }

  46. class Human extends Animal implements Genius
  47. {
  48.     String name;
  49.     Human(String n,int a,double w)
  50.     {
  51.             super(a,w);
  52.             name=n;
  53.     }
  54.     void showProfile()
  55.     {
  56.             System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  57.     }
  58.     void eat(double x)
  59.     {
  60.         System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
  61.         w+=x;
  62.     }
  63.     public void swim(double x)
  64.     {
  65.             System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  66.     }
  67.     public void sing(String x)
  68.     {
  69.             System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
  70.     }
  71.     public void takeCare()
  72.     {
  73.             System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
  74.     }
  75.     public void fly()
  76.     {
  77.             System.out.println(name+"以"+Genius.LEVEL+"水準,快速揮動兩片扇子,飛了起來!");        
  78.     }
  79. }
複製代碼

TOP

返回列表