java多態(翻譯自Java Tutorials)

來源:互聯網
上載者:User

原文出自 http://www.cnblogs.com/ggjucheng/archive/2012/11/26/2789638.html

多態,在字典中的定義是指在生物學的生物體或物種可以有許多不同的形式或階段。這一原則也適用於物件導向的程式設計語言如Java語言。子類可以定義自己獨特的行為,並共用父類一些相同的功能。

多態可以通過Bicycle類的修改進行展示。例如,可以添加一個printDescription方法,用來顯示當前在執行個體中的所有資料。

public void printDescription(){    System.out.println("\nBike is " + "in gear " + this.gear        + " with a cadence of " + this.cadence +        " and travelling at a speed of " + this.speed + ". ");}

為了展示java語言多態的特性,我們擴充Bicycle類: MountainBike和RoadBike類.例如MountainBike,添加一個變數suspension,這個是一個字串,它的值如果是Front指示這個這行車有前減震器,如果是Dual,指示單車有一前一後減震器。

更新後的類如下:

public class MountainBike extends Bicycle {    private String suspension;    public MountainBike(               int startCadence,               int startSpeed,               int startGear,               String suspensionType){        super(startCadence,              startSpeed,              startGear);        this.setSuspension(suspensionType);    }    public String getSuspension(){      return this.suspension;    }    public void setSuspension(String suspensionType) {        this.suspension = suspensionType;    }    public void printDescription() {        super.printDescription();        System.out.println("The " + "MountainBike has a" +            getSuspension() + " suspension.");    }} 

注意printDescription 方法已經被重載,除了之前的資訊,附加的suspension資訊也會被包含在輸出。

下一個,建立RoadBike類,由於公路單車或者賽跑單車都有細輪胎,添加一個屬性,跟蹤輪胎的寬頻,這裡是RoadBike的代碼:

public class RoadBike extends Bicycle{    // In millimeters (mm)    private int tireWidth;    public RoadBike(int startCadence,                    int startSpeed,                    int startGear,                    int newTireWidth){        super(startCadence,              startSpeed,              startGear);        this.setTireWidth(newTireWidth);    }    public int getTireWidth(){      return this.tireWidth;    }    public void setTireWidth(int newTireWidth){        this.tireWidth = newTireWidth;    }    public void printDescription(){        super.printDescription();        System.out.println("The RoadBike" + " has " + getTireWidth() +            " MM tires.");    }}

注意printDescription 方法已經被重載,這一次會顯示輪胎的寬度。

總結一下:現在有三個類: Bicycle, MountainBike和RoadBike。兩個子類重載了printDescription方法並輸出唯一資訊。

這裡是一個測試程式,建立三個Bicycle變數,每個變數賦值給三個bicycle類的每個執行個體,每個變數都會被輸出。

public class TestBikes {  public static void main(String[] args){    Bicycle bike01, bike02, bike03;    bike01 = new Bicycle(20, 10, 1);    bike02 = new MountainBike(20, 10, 5, "Dual");    bike03 = new RoadBike(40, 20, 8, 23);    bike01.printDescription();    bike02.printDescription();    bike03.printDescription();  }}

程式輸出如下:

Bike is in gear 1 with a cadence of 20 and travelling at a speed of 10.Bike is in gear 5 with a cadence of 20 and travelling at a speed of 10. The MountainBike has a Dual suspension.Bike is in gear 8 with a cadence of 40 and travelling at a speed of 20. The RoadBike has 23 MM tires.

Java虛擬機器(JVM)不調用對象執行個體的類定義的方法,而是調用對象執行個體適當的方法。這種行為被稱為虛擬方法調用,示範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.