返回列表 發帖

介面 (一)

本帖最後由 tonyh 於 2021-7-16 20:36 編輯

介面(Interface)是一種特殊的類別,在宣告時不是使用class而是改用interface,但在編譯後也會產生.class檔。介面只有宣告而沒有實作,架構上與抽象類別有點類似,用法與限制卻大不相同。

在Java中,類別並不允許多重繼承,但允許實作多個介面,而在介面繼承時,也允許繼承多個父介面,我們利用這兩種機制來模擬多重繼承。

我們可以把介面想像成物件與外界溝通的橋樑,或物件欲扮演的角色。通常我們使用介面來描述不同類別的物件間,共通的行為或特性。譬如:有個介面叫父親,把這個介面套用在小貓、小鳥、還是人類身上,就能使該物件開始扮演父親的角色,執行照顧子女的任務。

關於介面的幾個重點歸納如下:

1. 介面中的成員變數必須利用關鍵字final宣告為常數,並指定初始值。
2. 介面中的成員方法必須宣告為抽象方法,不能有方法主體。
3. 由於編譯器會自動將介面中的成員變數宣告為public、static與final,因此是常數變數,同時關鍵字可以省略。
4. 由於編譯器會自動將介面中的成員方法宣告為public與abstract,因此是抽象方法,同時關鍵字可以省略。
5. 類別可繼承一個類別但可實作多個介面。
6. 介面不能實作另一個介面但可繼承多個介面。
7. 實作介面的類別可以新增成員變數與成員方法,但一定要定義介面中所有抽象方法的內容,同時因為介面中的成員方法存取權限都是public,所以在這邊定義成員方法時,也必須加上public,否則會編譯錯誤。



[設計導引]

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. 讓類別 Human 實作 Swimmer, Singer, Father 這三個介面
    並實作所有抽象方法: swim(double x), sing(String song), takeCare()
5. 在 Human 類別下新增一物件, 並利用該物件帶出所有方法, 如執行畫面所示
  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.     Animal(int a,double w)
  17.     {
  18.             age=a;
  19.             this.w=w;
  20.     }
  21.     abstract void eat(double x);
  22.     abstract void showProfile();
  23. }

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

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

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

  39. class Human extends Animal implements Swimmer, Singer, Father
  40. {
  41.     String name;
  42.     Human(String n,int a,double w)
  43.     {
  44.             super(a,w);
  45.             name=n;
  46.     }
  47.     void showProfile()
  48.     {
  49.             System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  50.     }
  51.     void eat(double x)
  52.     {
  53.         System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
  54.         w+=x;
  55.     }
  56.     public void swim(double x)
  57.     {
  58.             System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  59.     }
  60.     public void sing(String x)
  61.     {
  62.             System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
  63.     }
  64.     public void takeCare()
  65.     {
  66.             System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
  67.     }   
  68. }
複製代碼

  1. public class Ch03 {

  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.         {
  18.                 age=a;
  19.                 this.w=w;
  20.         }
  21.         abstract void eat(double x);
  22.         abstract void showProfile();
  23. }

  24. class Human extends Animal implements Swimmer, Singer, Father
  25. {
  26.         String name;
  27.         Human(int a, double w, String n){
  28.                 super(a, w);
  29.                 name=n;
  30.         }
  31.         @Override
  32.         void eat(double x) {
  33.                 // TODO Auto-generated method stub
  34.                 System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
  35.         }

  36.         @Override
  37.         void showProfile() {
  38.                 // TODO Auto-generated method stub
  39.                 System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  40.         }
  41.         @Override
  42.         public void takeCare() {
  43.                 // TODO Auto-generated method stub
  44.                 System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
  45.         }
  46.         @Override
  47.         public void sing(String song) {
  48.                 // TODO Auto-generated method stub
  49.                 System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+song+".");
  50.         }
  51.         @Override
  52.         public void swim(double x) {
  53.                 // TODO Auto-generated method stub
  54.                 System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  55.         }
  56. }

  57. interface Swimmer
  58. {
  59.         String LEVEL="專業級";
  60.         void swim(double x);
  61. }

  62. interface Singer
  63. {
  64.         String LEVEL="專業級";
  65.         void sing(String song);
  66. }

  67. interface Father
  68. {
  69.         String LEVEL="新手級";
  70.         void takeCare();
  71. }
複製代碼

TOP

  1. public class CH04 {

  2.         public static void main(String[] args) {
  3.                 Human h=new Human(35,70,"湯湯");
  4.                 h.show();
  5.                 h.eat(0.85);
  6.                 h.show();
  7.                 h.swim(1500);
  8.                 h.sing("哈哈");
  9.                 h.care();
  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 show();
  22. }
  23. class Human extends Animal implements Swimmer,Singer,Father{
  24.         String name;
  25.         Human(int a, double w,String n) {
  26.                 super(a, w);
  27.                 name=n;
  28.         }

  29.         @Override
  30.         void eat(double x) {
  31.                 System.out.println(name+"咕咕咕吃了"+x+"公斤的食物");
  32.                 w+=x;
  33.         }

  34.         @Override
  35.         void show() {
  36.                 System.out.println(name+"今年"+age+"歲,體重"+w+"公斤");
  37.                
  38.         }

  39.         @Override
  40.         public void care() {
  41.                 System.out.println(name+"以"+Father.LEVEL+"的水準照顧小孩");
  42.                
  43.         }

  44.         @Override
  45.         public void sing(String s) {
  46.                 System.out.println(name+"以"+Singer.LEVEL+"的水準唱了"+s);
  47.                
  48.         }

  49.         @Override
  50.         public void swim(double x) {
  51.                 System.out.println(name+"以"+Swimmer.LEVEL+"的水準游了"+x+"公尺");
  52.                
  53.         }
  54.        
  55. }
  56. interface Swimmer{
  57.         String LEVEL="權威級";
  58.         void swim(double x);
  59. }
  60. interface Singer{
  61.         String LEVEL="權威級";
  62.         void sing(String s);
  63. }
  64. interface Father{
  65.         String LEVEL="入門級";
  66.         void care();
  67. }
複製代碼
hahahahahahahaha

TOP

本帖最後由 劉愷鈞 於 2021-7-23 19:02 編輯
  1. public class JPA01 {
  2.         public static void main(String args[])
  3.         {
  4.                 Human h=new Human(35, 70, "湯尼");
  5.                 h.showProfile();
  6.                 h.eat(0.85);
  7.                 h.showProfile();
  8.                 h.swim(1500);
  9.                 h.sing("新不了情");
  10.                 h.takeCare();
  11.         }
  12. }
  13. abstract class Animal
  14. {
  15.         int age;
  16.         double weight;
  17.         Animal(int a, int w)
  18.         {
  19.                 age=a;
  20.                 weight=w;
  21.         }
  22.         abstract void eat(double x);
  23.         abstract void showProfile();
  24. }
  25. class Human extends Animal implements Swimmer,Singer,Father
  26. {
  27.         String name;
  28.         Human(int a, int w,String n) {
  29.                 super(a, w);
  30.                 name=n;
  31.         }

  32.         void eat(double x) {
  33.                 System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物");
  34.                 weight+=x;
  35.         }

  36.         void showProfile() {
  37.                 System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");

  38.         }

  39.         public void swim(double x)
  40.         {
  41.                 System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  42.         }

  43.         public void sing(String song) {
  44.                 System.out.println(name+"以"+Singer.LEVEL+"水準,唱下了一首"+song+".");
  45.         }

  46.         public void takeCare()
  47.         {
  48.                 System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
  49.         }

  50. }
  51. interface Swimmer
  52. {
  53.         String LEVEL="專業級";       
  54.         void swim(double x);
  55. }
  56. interface Singer
  57. {
  58.         String LEVEL="專業級";       
  59.         void sing(String song);
  60. }
  61. interface Father
  62. {
  63.         String LEVEL="新手級";
  64.     void takeCare();
  65. }
複製代碼

TOP

  1. public class Ch65 {
  2.         public static void main(String[] args) {
  3.                 Human h1=new Human("湯尼",26,64);
  4.                 h1.showProfile();
  5.                 h1.eat(0.99);
  6.                 h1.showProfile();
  7.                 h1.swim(1000);
  8.                 h1.sing("忘情水");
  9.                 h1.takeCare();
  10.         }
  11. }

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

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

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

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

  39. class Human extends Animal implements Swimmer, Singer, Father
  40. {
  41.     String name;
  42.     Human(String n,int a,double w)
  43.     {
  44.             super(a,w);
  45.             name=n;
  46.     }
  47.     void showProfile()
  48.     {
  49.             System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  50.     }
  51.     void eat(double x)
  52.     {
  53.         System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
  54.         w+=x;
  55.     }
  56.     public void swim(double x)
  57.     {
  58.             System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  59.     }
  60.     public void sing(String x)
  61.     {
  62.             System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
  63.     }
  64.     public void takeCare()
  65.     {
  66.             System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
  67.     }   
  68. }
複製代碼
李宇澤Oscar

TOP

  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

  1. public class Ch05 {
  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("Afterglow");
  9.         h.takeCare();
  10.         }
  11. }
  12. abstract class Animal{
  13.         int age;
  14.         double w;
  15.         Animal(int a, double w){
  16.                 age=a;
  17.                 this.w=w;
  18.         }
  19.         abstract void eat(double x);
  20.         abstract void showProfile();
  21. }
  22. class Human extends Animal implements Swimmer, Father, Singer{
  23.     String name;
  24.         Human(int a, double w, String n) {
  25.                 super(a, w);
  26.                 name=n;
  27.         }

  28.         @Override
  29.         void eat(double x) {               
  30.                 System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
  31.                 w+=x;
  32.         }

  33.         @Override
  34.         void showProfile() {
  35.                 System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");               
  36.         }
  37.        
  38.         public void swim(double x){
  39.                 System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  40.         }
  41.         public void sing(String song){
  42.                 System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+song+".");
  43.         }
  44.         public void takeCare(){
  45.                 System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
  46.         }
  47. }
  48. interface Swimmer{
  49.         String LEVEL="專業級";
  50.         void swim(double x);
  51. }

  52. interface Singer{
  53.         String LEVEL="專業級";
  54.         void sing(String song);
  55. }

  56. interface Father{
  57.         String LEVEL="新手級";
  58.         void takeCare();
  59. }
複製代碼

TOP

  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.     Animal(int a,double w)
  17.     {
  18.             age=a;
  19.             this.w=w;
  20.     }
  21.     abstract void eat(double x);
  22.     abstract void showProfile();
  23. }

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

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

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

  39. class Human extends Animal implements Swimmer, Singer, Father
  40. {
  41.     String name;
  42.     Human(String n,int a,double w)
  43.     {
  44.             super(a,w);
  45.             name=n;
  46.     }
  47.     void showProfile()
  48.     {
  49.             System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  50.     }
  51.     void eat(double x)
  52.     {
  53.         System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
  54.         w+=x;
  55.     }
  56.     public void swim(double x)
  57.     {
  58.             System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  59.     }
  60.     public void sing(String x)
  61.     {
  62.             System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
  63.     }
  64.     public void takeCare()
  65.     {
  66.             System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
  67.     }   
  68. }
複製代碼

TOP

  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.         }

  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.         @Override
  31.         void eat(double x) {
  32.                 System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
  33.                 w+=x;
  34.         }
  35.     @Override
  36.         void showProfile() {
  37.             System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");       
  38.                
  39.         }
  40.         @Override
  41.         public void takeCare() {
  42.                 System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
  43.                
  44.         }
  45.         @Override
  46.         public void sing(String song) {
  47.                 System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+song+".");
  48.                
  49.         }
  50.         @Override
  51.         public void swim(double x) {
  52.                 System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  53.                
  54.         }
  55.    
  56. }
  57. interface Swimmer
  58. {
  59.         String LEVEL="專業級";
  60.         void swim(double x);
  61. }
  62. interface Singer
  63. {
  64.         String LEVEL="專業級";
  65.         void sing(String song);
  66. }
  67. interface Father
  68. {
  69.         String LEVEL="新手級";
  70.         void takeCare();
  71. }
複製代碼

TOP

  1. public class Ch9999 {

  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.         Animal(int a,double w)
  17.         {
  18.                 age=a;
  19.                 this.w=w;
  20.         }
  21.         abstract void eat(double x);
  22.         abstract void showProfile();
  23. }

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

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

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

  39. class Human extends Animal implements Swimmer, Singer, Father
  40. {
  41.         String name;
  42.         Human(String n,int a,double w)
  43.         {
  44.                 super(a,w);
  45.                 name=n;
  46.         }
  47.         void showProfile()
  48.         {
  49.                 System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  50.         }
  51.         void eat(double x)
  52.         {
  53.                 System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
  54.                 w+=x;
  55.         }
  56.         public void swim(double x)
  57.         {
  58.                 System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");
  59.         }
  60.         public void sing(String x)
  61.         {
  62.                 System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");
  63.         }
  64.         public void takeCare()
  65.         {
  66.                 System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");
  67.         }   
  68. }
複製代碼

TOP

  1. public class Ch01 {

  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.         int age;
  14.         double w;
  15.         Animal(int a, double w){
  16.             a=age;
  17.             this.w=w;
  18.         }
  19.         abstract void eat(double x);
  20.         abstract void showProfile();
  21. }
  22. class Human extends Animal implements Swimmer, Father, Singer{
  23.         String name;
  24.         Human(String n, int a, double w) {
  25.                 super(a, w);
  26.                 name=n;
  27.         }
  28.         void eat(double x){
  29.                 System.out.println(name+"咕嚕咕嚕吃下了"+x+"公斤的食物.");
  30.         w+=x;
  31.         }
  32.         void showProfile(){
  33.                 System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  34.         }
  35.         @Override
  36.         public void sing(String x) {
  37.                 System.out.println(name+"以"+Singer.LEVEL+"水準,唱了一首"+x+".");               
  38.         }
  39.         @Override
  40.         public void takeCare() {
  41.                 System.out.println(name+"以"+Father.LEVEL+"水準,開始扮演父親的角色,照顧小孩.");       
  42.         }
  43.         @Override
  44.         public void swim(double x) {
  45.                 System.out.println(name+"以"+Swimmer.LEVEL+"水準,刷刷刷快速游了"+x+"公尺.");               
  46.         }
  47.        
  48. }

  49. interface Father{
  50.         String LEVEL="新手級";
  51.         public abstract void takeCare();
  52. }
  53. interface Singer{
  54.         String LEVEL="專業級";
  55.         public abstract void sing(String x);
  56. }
  57. interface Swimmer{
  58.         String LEVEL="專業級";
  59.         public abstract void swim(double x);
  60. }
複製代碼

TOP

返回列表