您好,登錄后才能下訂單哦!
在C#中,由于語言本身不支持多繼承,但可以通過接口、委托、混合類等方式來模擬多繼承的效果。
interface IFly
{
void Fly();
}
interface ISwim
{
void Swim();
}
class Bird : IFly
{
public void Fly()
{
Console.WriteLine("Bird can fly");
}
}
class Fish : ISwim
{
public void Swim()
{
Console.WriteLine("Fish can swim");
}
}
class Duck : IFly, ISwim
{
public void Fly()
{
Console.WriteLine("Duck can fly");
}
public void Swim()
{
Console.WriteLine("Duck can swim");
}
}
class Fly
{
public void FlyAction()
{
Console.WriteLine("Can fly");
}
}
class Swim
{
public void SwimAction()
{
Console.WriteLine("Can swim");
}
}
class Duck
{
private Fly fly = new Fly();
private Swim swim = new Swim();
public void Fly()
{
fly.FlyAction();
}
public void Swim()
{
swim.SwimAction();
}
}
class Fly
{
public void FlyAction()
{
Console.WriteLine("Can fly");
}
}
class Swim
{
public void SwimAction()
{
Console.WriteLine("Can swim");
}
}
class Duck
{
private Fly fly = new Fly();
private Swim swim = new Swim();
public void Fly()
{
fly.FlyAction();
}
public void Swim()
{
swim.SwimAction();
}
}
通過以上方式,可以在C#中模擬多繼承的效果,實現(xiàn)類的復(fù)用和功能的擴展。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。