您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。