Java中的抽象

來源:互聯網
上載者:User

標籤:within   stat   bae   不同   沒有   code   action   rac   news   

以下內容引用自http://wiki.jikexueyuan.com/project/java/abstraction.html:

Abstraction是指在OOP中讓一個類抽象的能力。一個抽象類別是不能被執行個體化的。類的功能仍然存在,它的欄位,方法和建構函式都以相同的方式進行訪問。只是不能建立一個抽象類別的執行個體。

如果一個類是抽象的,即不能被執行個體化,這個類如果不是子類它將沒有什麼作用。這體現了在設計過程中抽象類別是如何被提出的。一個父類包含子類的準系統集合,但是父類是抽象的,不能自己去使用功能。

一、抽象類別

使用關鍵字abstract來聲明一個抽象類別。它出現在關鍵字class的前面。

/* File name : Employee.java */public abstract class Employee{   private String name;   private String address;   private int number;   public Employee(String name, String address, int number)   {      System.out.println("Constructing an Employee");      this.name = name;      this.address = address;      this.number = number;   }   public double computePay()   {     System.out.println("Inside Employee computePay");     return 0.0;   }   public void mailCheck()   {      System.out.println("Mailing a check to " + this.name       + " " + this.address);   }   public String toString()   {      return name + " " + address + " " + number;   }   public String getName()   {      return name;   }   public String getAddress()   {      return address;   }   public void setAddress(String newAddress)   {      address = newAddress;   }   public int getNumber()   {     return number;   }}

注意, Employee類沒什麼不同。類現在是抽象的,但它仍然有三個欄位,七個方法,一個建構函式。

現在如果嘗試以下代碼:

/* File name : AbstractDemo.java */public class AbstractDemo{   public static void main(String [] args)   {      /* Following is not allowed and would raise error */      Employee e = new Employee("George W.", "Houston, TX", 43);      System.out.println("\n Call mailCheck using Employee reference--");      e.mailCheck();    }}//當進行編譯時間,將得到下面的錯誤:Employee.java:46: Employee is abstract; cannot be instantiated      Employee e = new Employee("George W.", "Houston, TX", 43);                   ^1 error

二、繼承抽象類別

可以如下繼承Employee類:

/* File name : Salary.java */public class Salary extends Employee{   private double salary; //Annual salary   public Salary(String name, String address, int number, double      salary)   {       super(name, address, number);       setSalary(salary);   }   public void mailCheck()   {       System.out.println("Within mailCheck of Salary class ");       System.out.println("Mailing check to " + getName()       + " with salary " + salary);   }   public double getSalary()   {       return salary;   }   public void setSalary(double newSalary)   {       if(newSalary >= 0.0)       {          salary = newSalary;       }   }   public double computePay()   {      System.out.println("Computing salary pay for " + getName());      return salary/52;   }}

這裡,不能執行個體化一個新的Employee,但是如果執行個體化一個新的Salary對象,Salary對象將繼承Employee 的三個欄位和七個方法。

/* File name : AbstractDemo.java */public class AbstractDemo{   public static void main(String [] args)   {      Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);      Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);      System.out.println("Call mailCheck using Salary reference --");      s.mailCheck();      System.out.println("\n Call mailCheck using Employee reference--");      e.mailCheck();    }}//這將產生如下結果:Constructing an EmployeeConstructing an EmployeeCall mailCheck using  Salary reference --Within mailCheck of Salary classMailing check to Mohd Mohtashim with salary 3600.0Call mailCheck using Employee reference--Within mailCheck of Salary classMailing check to John Adams with salary 2400.

三、抽象方法

如果想一個提供特定方法的類,但是想要在他的子類中實際實現這個方法,可以在父類中聲明這個方法為抽象的。

abstract關鍵字也被用來定義抽象方法。一個抽象方法是有方法簽名的但沒有方法實體。

抽象方法無需定義,並且它的方法名以分號結束,不需要花括弧。

public abstract class Employee{   private String name;   private String address;   private int number;   public abstract double computePay();   //Remainder of class definition}

聲明一個抽象方法有兩個結果:

  • 如果一個類中含有一個抽象方法,類必須也是抽象的。
  • 任何一個子類必須覆蓋這個抽象方法,或者繼續將它聲明為抽象方法。

子類繼承一個抽象方法,必須要去覆蓋他。如果不這樣做的話,它們必須將其繼續聲明為抽象,或在它們的子類中去覆蓋它們。

最終,後代類不得不去實現抽象方法;否則會一直有一個不能被執行個體化的抽象類別。

如果Salary繼承Employee類,則他必須如下要去實現computerPay()方法:

/* File name : Salary.java */public class Salary extends Employee{   private double salary; // Annual salary   public double computePay()   {      System.out.println("Computing salary pay for ");      return salary/52;   }   //Remainder of class definition}

 

 測試工程:https://github.com/easonjim/5_java_example/tree/master/javabasicstest/test20

Java中的抽象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.