Board logo

標題: 介面 (二) [打印本頁]

作者: tonyh    時間: 2023-4-8 15:37     標題: 介面 (二)

本帖最後由 tonyh 於 2023-4-8 16:06 編輯



[設計導引]

1. 定義抽象類別 Animal, 該類別包含了
    兩個屬性成員: age 與 w
    建構子
    兩個抽象方法: eat(double x) 與 showProfile()
2. 定義一般類別 Human 繼承 Animal, 在該類別中新增
    一個屬性成員: name
    建構子
    實作自父類別繼承下來的兩個抽象方法: eat(double x) 與 showProfile()
3. 定義三個介面: Swimmer, Singer, Father
    分別包含了常數變數 LEVEL="專業級" 或 "新手級" 等
    以及 swim(double x), sing(String song), takeCare() 等抽象方法
4. 定義一個介面: Genius 多重繼承 Swimmer, Singer, Father 這三個介面
    在該介面中新增一抽象方法 fly()
5. 讓類別 Human 實作介面 Genius
    並實作所有抽象方法: swim(double x), sing(String song), takeCare(), fly()
6. 在 Human 類別下新增一物件, 並利用該物件帶出所有方法, 如執行畫面所示
  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.     abstract void eat(double x);
  23.     abstract void showProfile();
  24. }

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

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

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

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

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

作者: 鄞美旭    時間: 2023-4-8 16:25

  1. public class A {

  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.                 h.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.         abstract void eat(double x);
  23.         abstract void showProfile();
  24. }
  25. interface Swimmer
  26. {
  27.         String LEVEl="專業級";
  28.         void swim(double x);
  29. }
  30. interface Singer
  31. {
  32.         String LEVEL="專業級";
  33.         void sing(String song);
  34. }
  35. interface Father
  36. {
  37.         String LEVEL="新手級";
  38.         void takeCare();
  39. }
  40. interface Genius extends Swimmer,Singer, Father
  41. {
  42.         String LEVEL="大神級";
  43.         void fly();
  44. }
  45. class Human extends Animal implements Genius
  46. {
  47.         String name;
  48.         Human(String n, int a, double w)
  49.         {
  50.                 super(a, w);
  51.                 name=n;
  52.         }
  53.         @Override
  54.         void eat(double x) {
  55.                 System.out.println(name+"咕嚕咕嚕吃下"+x+"公斤的食物。");
  56.                 w=w+x;
  57.         }
  58.         @Override
  59.         void showProfile() {
  60.                 System.out.println(name+"今年"+age+"歲,體重"+w+"公斤。");
  61.         }
  62.         @Override
  63.         public void swim(double x) {
  64.                 System.out.println(name+"以"+Swimmer.LEVEl+"水準,刷刷刷快速地遊了"+x+"公尺。");
  65.         }
  66.         @Override
  67.         public void sing(String song) {
  68.                 System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+song+"。");
  69.         }
  70.         @Override
  71.         public void takeCare() {
  72.                 System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩。");
  73.         }
  74.         @Override
  75.         public void fly() {
  76.                 System.out.println(name+"以"+Genius.LEVEL+"水準,快速揮動兩片扇子,飛了起來!");
  77.         }
  78. }
複製代碼

作者: 夏子甯    時間: 2023-4-8 16:26

  1. public class Ch01 {

  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.                 h.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.         abstract void eat(double x);
  23.         abstract void showProfile();
  24. }
  25. interface Swimmer
  26. {
  27.         String LEVEL="專業級";
  28.         void swim(double x);
  29. }
  30. interface Singer
  31. {
  32.         String LEVEL="專業級";
  33.         void sing(String x);
  34. }
  35. interface Father
  36. {
  37.         String LEVEL="新手級";
  38.         void takeCare();
  39. }
  40. interface Genius extends Swimmer, Singer,Father
  41. {
  42.         String LEVEL="大神級";
  43.         void fly();
  44. }
  45. class Human extends Animal implements Genius
  46. {
  47.         String name;
  48.         Human(String n,int a,double w)
  49.         {
  50.                 super(a,w);
  51.                 name=n;
  52.         }
  53.         void showProfile()
  54.         {
  55.                 System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  56.         }
  57.         void eat(double x)
  58.         {
  59.                  System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
  60.                  w+=x;
  61.         }
  62.         @Override
  63.         public void swim(double x) {
  64.                 System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  65.         }
  66.         @Override
  67.         public void sing(String x) {
  68.                 System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
  69.         }
  70.         @Override
  71.         public void takeCare() {
  72.                 System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");       
  73.         }
  74.         @Override
  75.         public void fly() {
  76.                 System.out.println(name+"以"+Genius.LEVEL+"水準,快速揮動兩片扇子,飛了起來!");        
  77.         }
  78. }
複製代碼

作者: 秉宏1    時間: 2023-4-8 16:27

  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. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/) Powered by Discuz! 7.2