返回列表 發帖

抽象類別

本帖最後由 tonyh 於 2023-3-18 16:49 編輯

在設計Java程式時,當程式設計者希望基礎類別只是用來被衍生類別繼承,而不希望真正產生一個基礎類別的物件,此時就可以將基礎類別宣告成抽象類別。

所謂抽象類別(Abstract Class)是指無法建立物件,只能被衍生類別繼承的一種特殊類別,而在抽象類別裡可以宣告抽象方法(Abstract Method),抽象方法是一個尚未完全實作的方法,表示一個方法原型,必須在衍生類別中撰寫方法的實作內容。

關於抽象類別的幾個重點歸納如下:

1. 包含抽象方法的類別一定要宣告為抽象類別。
2. 抽象類別有建構子,但無法產生物件實體。
3. 抽象類別可同時包含抽象方法與一般方法,也可以完全沒有抽象方法。
4. 抽象類別一定要被繼承,而抽象方法一定要被改寫。
5. 若衍生類別中有任何一個抽象方法沒有被實作,則必須將該類別也宣告為抽象類別,以便強迫更下層的類別在繼承它後,實作剩餘沒有被實作的抽象方法。

  1. public class Ch71
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         Square sq=new Square("方形1號","綠色",5);
  6.         sq.showName();
  7.         sq.showColor();
  8.         sq.calArea();
  9.         Tri tr=new Tri("三角形1號","粉紅色",7,5);
  10.         tr.showName();
  11.         tr.showColor();
  12.         tr.calArea();
  13.     }   
  14. }

  15. abstract class Shape
  16. {
  17.     String name, color;
  18.     Shape(String name, String color)
  19.     {
  20.         this.name=name;
  21.         this.color=color;
  22.     }
  23.     void showName()
  24.     {
  25.         System.out.println("物件名稱: "+name);
  26.     }
  27.     void showColor()
  28.     {
  29.         System.out.println("顏色為: "+color);
  30.     }
  31.     abstract void calArea();
  32. }

  33. class Square extends Shape
  34. {
  35.     int x;
  36.     Square(String name, String color, int x)
  37.     {
  38.         super(name,color);
  39.         this.x=x;
  40.     }  
  41.     void calArea()
  42.     {
  43.         System.out.println("面積為: "+x*x+"平方公分");     
  44.     }
  45. }

  46. class Tri extends Shape
  47. {
  48.     double x, y;
  49.     Tri(String name, String color, double x, double y)
  50.     {
  51.         super(name,color);
  52.         this.x=x;
  53.         this.y=y;
  54.     }  
  55.     void calArea()
  56.     {
  57.         System.out.println("面積為: "+(x*y/2)+"平方公分");     
  58.     }
  59. }
複製代碼

  1. public class CH01
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         Square sq=new Square("方形1號","綠色",5);
  6.         sq.showName();
  7.         sq.showColor();
  8.         sq.calArea();
  9.         Tri tr=new Tri("三角形1號","粉紅色",7,5);
  10.         tr.showName();
  11.         tr.showColor();
  12.         tr.calArea();
  13.     }   
  14. }

  15. abstract class Shape
  16. {
  17.     String name, color;
  18.     Shape(String name, String color)
  19.     {
  20.         this.name=name;
  21.         this.color=color;
  22.     }
  23.     void showName()
  24.     {
  25.         System.out.println("物件名稱: "+name);
  26.     }
  27.     void showColor()
  28.     {
  29.         System.out.println("顏色為: "+color);
  30.     }
  31.     abstract void calArea();
  32. }

  33. class Square extends Shape
  34. {
  35.     int x;
  36.     Square(String name, String color, int x)
  37.     {
  38.         super(name,color);
  39.         this.x=x;
  40.     }  
  41.     void calArea()
  42.     {
  43.         System.out.println("面積為: "+x*x+"平方公分");     
  44.     }
  45. }

  46. class Tri extends Shape
  47. {
  48.     double x, y;
  49.     Tri(String name, String color, double x, double y)
  50.     {
  51.         super(name,color);
  52.         this.x=x;
  53.         this.y=y;
  54.     }  
  55.     void calArea()
  56.     {
  57.         System.out.println("面積為: "+(x*y/2)+"平方公分");     
  58.     }
  59. }
複製代碼

TOP

  1. public class B{
  2.         public static void main(String[] args) {
  3.                 Square sq=new Square("方形1號", "綠色", 5);
  4.                 sq.showName();
  5.                 sq.showColor();
  6.                 sq.calArea();
  7.                
  8.                 Tri tr=new Tri("三角形1號", "粉紅色" ,7,5);
  9.                 tr.showName();
  10.                 tr.showColor();
  11.                 tr.calArea();
  12.         }
  13. }
  14. abstract class Shape
  15. {
  16.         String name, color;
  17.        
  18.         Shape(String n,String c)
  19.         {
  20.                 name=n;
  21.                 color=c;
  22.         }
  23.         void showName()
  24.         {
  25.                 System.out.println("物件名稱:"+name);
  26.         }
  27.         void showColor()
  28.         {
  29.                 System.out.println("顏色為:"+color);
  30.         }
  31.         abstract void calArea();
  32. }
  33. class Square extends Shape
  34. {
  35.         int x;
  36.         Square(String n, String c, int x){
  37.                 super(n, c);
  38.                 this.x=x;
  39.         }
  40.         @Override
  41.         void calArea() {
  42.                 System.out.println("面積為:"+x*x+"平方公分");
  43.         }
  44.        
  45. }
  46. class Tri extends Shape
  47. {
  48.         double x, y;
  49.         Tri(String n, String c, double x, double y){
  50.                 super(n, c);
  51.                 this.x=x;
  52.                 this.y=y;
  53.         }
  54.         @Override
  55.         void calArea() {
  56.                 System.out.println("面積為:"+x*y/2+"平方公分");
  57.         }
  58.        
複製代碼

TOP

  1. public class Ch02 {

  2.         public static void main(String[] args) {
  3.             Square sq=new Square("方形1號","綠色",5);
  4.             sq.showName();
  5.             sq.showColor();
  6.             sq.calArea();
  7.             
  8.             Tri tr=new Tri("三角形1號","粉紅色",7,5);
  9.             tr.showName();
  10.             tr.showColor();
  11.             tr.calArea();
  12.         }
  13. }
  14. abstract class Shape
  15. {
  16.         String name,color;
  17.        
  18.         Shape(String n,String c)
  19.         {
  20.                 name=n;
  21.                 color=c;
  22.         }
  23.         void showName()
  24.         {
  25.                 System.out.println("物件名稱:"+name);
  26.         }
  27.         void showColor()
  28.         {
  29.                 System.out.println("顏色為:"+color);
  30.         }
  31.        
  32.         abstract void calArea();
  33. }

  34. class Square extends Shape
  35. {
  36.         int x;
  37.         Square(String n,String c,int x){
  38.                 super(n,c);
  39.                 this.x=x;
  40.         }
  41.         @Override
  42.         void calArea() {
  43.                         System.out.println("面積為:"+x*x+"平方公分");
  44.         }
  45.        
  46. }

  47. class Tri extends Shape
  48. {
  49.         double x,y;
  50.         Tri(String n,String c,double x,double y){
  51.                 super(n,c);
  52.                 this.x=x;
  53.                 this.y=y;
  54.         }
  55.         @Override
  56.         void calArea() {
  57.                         System.out.println("面積為:"+x*y/2+"平方公分");
  58.         }
  59.        
  60. }
複製代碼

TOP

返回列表