黑馬程式員-------介面和抽象類別

來源:互聯網
上載者:User

標籤:

/**
* @author Administrator
*
* @description 介面測試類別
* @history
*/
interface Interface{
public static final String INFO = "helloeclipse";
public void print(); // 定義方法
public abstract void printInfo();
}
class I implements Interface{
public void print() {
// 方法實現
System.out.println(INFO); // helloeclipse
}
public void printInfo() {
// ...
}
}
public class InterfaceTestDemo {
/***@description
*@param args
*/
public static void main(String[] args) {
// 介面interface
// 介面interface和抽象類別abstract一樣不能直接執行個體化必須通過子類來實現
new Interface(){
public void print() {
// 匿名內部類,方法實現
System.out.println(INFO); // helloeclipse
}
public void printInfo() {
// ...
}
};
// 介面和抽象類別對比,介面更像一種規範、抽象類別更像一種模板
// 抽象類別定義一些公用的實現,以及一些未實現的方法交給子類具體實現特定的功能-模板方法
}
}
/**
* @author Administrator
*
* @description 抽象類別學習測試類別
* @history
*/
abstract class Abstract{ // 定義抽象類別
public final static String INFO = "helloeclipse"; // 定義全域變數/常量
abstract public void print(); // 定義抽象方法
// 抽象類別不能直接執行個體化,抽象類別的子類如果不是抽象類別那麼一定要實現所有的抽象方法
}
class AbstractZiLei extends Abstract{ // 定義抽象子類
public void print() { // 實現子類方法
System.out.println(INFO);
}
}
public class AbstractTestDemo {
/**
*@description
*@param args
*/
public static void main(String[] args) {
// 介面和抽象類別interface,abstract
// 抽象類別:含有抽象方法的類叫做抽象類別用abstract修飾的
new Abstract(){public void print() {
System.out.println(INFO); // 匿名抽象類別實現
}
};
// 抽象類別能不能被final修飾呢?final定義的類為終結類,抽象類別是需要子類來實現的
// final abstract class Abstract{ //... } // 編譯報錯
}
}

黑馬程式員-------介面和抽象類別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.