返回列表 發帖
以諾,你有用到interface嗎??

TOP

  1. package ch80;

  2. public class ch80 {

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

  13. }
  14. abstract class Animal
  15. {
  16.     int age;
  17.     double w;
  18.     Animal(int age,double w)
  19.     {
  20.             this.age=age;
  21.             this.w=w;
  22.     }
  23.     abstract void eat(double x);
  24.     abstract void showProfile();
  25. }
  26. interface Swimmer
  27. {
  28.     String level="專業級";       
  29.     void swim(double w);
  30. }
  31. interface Singer
  32. {
  33.     String level="專業級";       
  34.     void swim(String song);
  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 name,int age,double w)
  45.     {
  46.             super(age,w);
  47.             this.name=name;
  48.     }
  49.     void eat(double x)
  50.     {
  51.             System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物");
  52.             w+=x;
  53.     }
  54.     void showProfile()
  55.     {
  56.             System.out.println(name+"今年"+age+"歲,體重"+w+"公斤");
  57.     }
  58.     public void swim(double x)
  59.     {
  60.         System.out.println(name+"以"+Swimmer.level+"水準,刷刷刷地快速游了"+x+"公尺");
  61.     }
  62.     public void sing(String song)
  63.     {
  64.         System.out.println(name+"以"+Singer.level+"水準,唱了"+song);
  65.     }
  66.     public void takeCare()
  67.     {
  68.         System.out.println(name+"以"+Father.level+"開始扮演父親的腳色照顧小孩");
  69.     }
  70. }
複製代碼

TOP

返回列表