抽象类可以实现接口吗? [英] Can an abstract class implement an interface?

查看:257
本文介绍了抽象类可以实现接口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抽象类可以实现接口吗?


抽象类可以从接口实现.
在这种情况下,我们必须为从接口实现的所有方法提供方法主体.


使用系统;
接口IBaseInterface
{
void Function1();
}

抽象类AbsClass:IBaseInterface
{
public void Function1()
{
Console.WriteLine(从IBaseInterface实现的功能");
}
}

class DerivedClass:AbsClass
{
}
类MainClient
{
公共静态void Main()
{
MainClass mc =新的MainClass();
mc.Function1();
}
}
}
}

Can an abstract class implement an interface?


An abstract class can implement from an interface.
In this case, we must provide method body for all methods it implemented from the interface.


using System;
interface IBaseInterface
{
void Function1();
}

abstract class AbsClass : IBaseInterface
{
public void Function1()
{
Console.WriteLine("Function implemented from the IBaseInterface");
}
}

class DerivedClass : AbsClass
{
}
class MainClient
{
public static void Main()
{
MainClass mc = new MainClass();
mc.Function1();
}
}
}
}

推荐答案

我认为您编写的代码还可以,但是如果您想强制开发人员在DerivedClass中实现该接口,那么我认为它不会工作
但如果它是可选的,则可以在DerivedClass中对其进行修改

另一个方法是,您可以实现接口并继承派生类中的基类,现在您可以获得2个对象的功能
I think the code you wrote is ok but if you want to force the developer to implement the interface in the DerivedClass then i think it''s not gonna work
but if it''s optional you can ovveride it in the DerivedClass

Another senario you can implement the interface and inherit the base class in the derived class and now you get the power of the 2 objects


这篇关于抽象类可以实现接口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆