您好,登錄后才能下訂單哦!
在C#中,由于語言本身不支持多繼承,可以通過使用接口(interface)、委托(delegates)和擴展方法(extension methods)等方式來模擬多繼承的需求。
interface IFlyable
{
void Fly();
}
interface ISwimmable
{
void Swim();
}
class Bird : IFlyable
{
public void Fly()
{
Console.WriteLine("Bird is flying.");
}
}
class Fish : ISwimmable
{
public void Swim()
{
Console.WriteLine("Fish is swimming.");
}
}
class Duck : IFlyable, ISwimmable
{
public void Fly()
{
Console.WriteLine("Duck is flying.");
}
public void Swim()
{
Console.WriteLine("Duck is swimming.");
}
}
delegate void FlyDelegate();
delegate void SwimDelegate();
class Bird
{
FlyDelegate flyDelegate;
public Bird(FlyDelegate flyDelegate)
{
this.flyDelegate = flyDelegate;
}
public void Fly()
{
flyDelegate();
}
}
class Fish
{
SwimDelegate swimDelegate;
public Fish(SwimDelegate swimDelegate)
{
this.swimDelegate = swimDelegate;
}
public void Swim()
{
swimDelegate();
}
}
static class BirdExtensions
{
public static void Fly(this Bird bird)
{
Console.WriteLine("Bird is flying.");
}
}
static class FishExtensions
{
public static void Swim(this Fish fish)
{
Console.WriteLine("Fish is swimming.");
}
}
class Bird { }
class Fish { }
class Duck : Bird, Fish
{
public void Fly()
{
this.Fly();
}
public void Swim()
{
this.Swim();
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。