您好,登錄后才能下訂單哦!
C#語言本身不支持多繼承,但可以通過接口、組合等方式來實現類似的功能。
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");
}
}
class Engine
{
public void Start()
{
Console.WriteLine("Engine is starting");
}
}
class Car
{
private Engine engine = new Engine();
public void Start()
{
engine.Start();
Console.WriteLine("Car is starting");
}
}
通過以上兩種方式,可以模擬實現類似多繼承的效果。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。