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

  12. abstract class Animal
  13. {
  14.         int age;
  15.         double w;
  16.         Animal(int a, double w) {
  17.                 age=a;
  18.                 this.w=w;
  19.         }
  20.         abstract void eat(double x);
  21.         abstract void showProfile();
  22. }

  23. class Human extends Animal implements Swimmer, Singer, Father
  24. {
  25.         String name;
  26.         Human(int a, double w, String n) {
  27.                 super(a, w);
  28.                 name=n;
  29.         }
  30.         void eat(double x) {
  31.                 System.out.println(name+"咕嚕咕嚕吃不下了"+x+"公斤。");
  32.                 w+=x;
  33.         }
  34.         void showProfile() {
  35.                 System.out.println(name+"今年"+age+"歲,體重"+w+"公斤。");
  36.         }
  37.         public void swim(double x)
  38.         {
  39.                 System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  40.         }
  41.         public void sing(String x)
  42.         {
  43.                 System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
  44.         }
  45.         public void takeCare()
  46.         {
  47.                 System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
  48.         }
  49. }

  50. interface Swimmer
  51. {
  52.         String LEVEL="專業級";
  53.         void swim(double x);
  54. }

  55. interface Singer
  56. {
  57.         String LEVEL="專業級";
  58.         void sing(double song);
  59. }

  60. interface Father
  61. {
  62.         String LEVEL="專業級";
  63.         void takeCare();
  64. }
複製代碼

TOP

返回列表