JAVA學習筆記-抽象類別

來源:互聯網
上載者:User

標籤:xtend   不能   如何   rgs   學習筆記   extend   void   his   通用   

-為什麼需要抽象類別?如何定義抽象類別?

  ·是一種模板模式。抽象類別為所有子類提供了一個通用模板,子類可以在這個模板基礎上進行擴充。

  ·通過抽象類別,可以避免子類設計的隨意性。通過抽象類別,我們就可以做到嚴格限制子類的設計,使子類之間更加通用。

-要點:

√  有抽象方法的類智能定義能抽象類別

√  抽象類別不能執行個體化,及不能用new來執行個體化抽象類別。

√  抽象類別可以包含屬性、方法、構造方法。但是構造方法不能用來new執行個體,只能用來被子類調用。

√  抽象類別只能用來繼承。

√  抽象方法必須被子類實現。

 

Eg:

package Study;

public abstract class TestAbstract {
  public abstract void run();
  public abstract void eat();
  public void doSomething(){
  System.out.println("做點什麼事都好");
  eat();
}

public static void main(String[] args){
  TestAbstract thing = new catAnimal();
  thing.run();
}
}
class catAnimal extends TestAbstract {
  public void run(){
    System.out.println("我可以走貓步");
    this.doSomething();
  }
  public void eat(){
  System.out.println("我可以吃貓食");
  }
}

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.