返回列表 發帖
  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(10);
  8.         h1.sing("456");
  9.         h1.takeCare();

  10.         }

  11. }
  12. abstract class Animal
  13. {
  14.         int age;
  15.         double we;
  16.         Animal(int a,double w)
  17.         {
  18.                 age=a;
  19.                 we=w;
  20.         }
  21.         abstract void eat(double x);
  22.         abstract void showProfile();
  23.        
  24. }
  25. interface Swimmer {
  26.    String LEVEL ="專業級";
  27.         void swim(double x);
  28. }
  29. interface Singer {
  30.         String LEVEL ="專業級";
  31.         void sing(String s);
  32.        
  33. }
  34. interface Father {
  35.         String LEVEL ="新手級";
  36.         void takeCare();
  37. }
  38. class Human extends Animal implements Swimmer ,Singer ,Father
  39. {
  40.         String name;
  41.        
  42.         Human(String n,int a,double w)
  43.         {
  44.                 super(a,w);
  45.                 name=n;
  46.                
  47.         }
  48.        
  49.                
  50.        


  51.         @Override
  52.         void eat(double x) {
  53.                 System.out.println(name+"吃下了"+x+"公斤的食物");
  54.                 we+=x;
  55.         }
  56.         @Override
  57.         void showProfile() {
  58.                 System.out.println(name+"今年"+age+"歲 體重"+we+"公斤");
  59.                
  60.         }
  61.        
  62.                
  63.         @Override
  64.         public void takeCare() {
  65.                 System.out.println(name+"以"+Father.LEVEL+"開始扮演父親的角色,照顧小孩");
  66.                
  67.         }
  68.         @Override
  69.         public void sing(String s) {
  70.                 System.out.println(name+"以"+Singer.LEVEL+"的水準,唱了一首"+s);
  71.                
  72.         }
  73.         @Override
  74.         public void swim(double x) {
  75.                 System.out.println(name+"以"+Swimmer.LEVEL+"的水準,快速地游了"+x+"公尺");
  76.                
  77.         }



  78.        
  79.                
  80. }
複製代碼

TOP

返回列表