返回列表 發帖
  1. public class Ch72 {
  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.         }
  11. }

  12. abstract class Animal
  13. {
  14.     int age;
  15.     double w;
  16.    
  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. class Human extends Animal implements Swimmer, Singer, Father
  42. {
  43.     String name;
  44.     Human(String n,int a,double w)
  45.     {
  46.             super(a,w);
  47.             name=n;
  48.     }
  49.    
  50.     void showProfile()
  51.     {
  52.             System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  53.     }
  54.    
  55.     void eat(double x)
  56.     {
  57.         System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
  58.         w+=x;
  59.     }
  60.    
  61.     public void swim(double x)
  62.     {
  63.             System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  64.     }
  65.    
  66.     public void sing(String x)
  67.     {
  68.             System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
  69.     }
  70.    
  71.     public void takeCare()
  72.     {
  73.             System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
  74.     }   
  75. }
複製代碼
我是嘉禾豬   我是嘉禾豬   我是嘉禾豬

TOP

返回列表