java中的多重繼承

來源:互聯網
上載者:User

標籤:can   bsp   void   介面   static   編程   java編程思想   rgs   class   

interface CanFight
{
void fight();
}
interface CanSwim
{
void swim();
}
interface CanFly
{
void fly();
}
class ActionCharacter
{
public void fight()
{

}
}
class Hero extends ActionCharacter

implements CanFight,CanSwim,CanFly
{
public void swim()
{

}
public void fly()
{

}
}

public class Adventure
{
public static void t(CanFight x)
{
x.fight();
}
public static void u(CanSwim x)
{
x.swim();
}
public static void v(CanFly x)
{
x.fly();
}
public static void w(ActionCharacter x)
{
x.fight();
}
public static void main(String[] args)
{
Hero h=new Hero();
t(h);
u(h);
v(h);
w(h);
}

}

 


/*
* 如果要從一個非介面的類繼承,那麼只能從一個類去繼承。其餘的基元素都必須是介面,需要將所有的介面名都置於implements關鍵字之後,用逗號將他們一一隔開,
* 可以繼承任意多個介面,並可以向上轉型為每個介面,因為每個介面都是獨立的類型。
*
*
*
*
* 使用介面的核心原因是為了能夠向上轉型為多個基底類型(以及由此而帶來的靈活性),然而,使用介面的第二個原因,卻是與使用抽象基類相同:防止用戶端程式員建立該類的對象
* 並確保這僅僅是建立一個介面。
*
* 那我們什麼時候使用介面,什麼時候使用抽象類別呢?
* 如果要建立不帶任何方法的定義和成員變數的基類,那就應該選擇介面,
*
*
*
*
* -----摘自《java編程思想》
*
*
*
* */

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.