Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#接口和继承的问题_C#_Unity3d_Inheritance_Interface_Properties - Fatal编程技术网

C#接口和继承的问题

C#接口和继承的问题,c#,unity3d,inheritance,interface,properties,C#,Unity3d,Inheritance,Interface,Properties,我想在类中实现以下接口: public interface IAgro { AgroState agroState { get; } } 问题是,我希望我的属性实现从AgroState继承的另一个类,而不是使用接口在类中实现AgroState public class E1_AgroState : AgroState { ... } public class BasicWalkingEnemy : Entity, IAgro { public E1_AgroState ag

我想在类中实现以下接口:

public interface IAgro {
     AgroState agroState { get; }
}
问题是,我希望我的属性实现从AgroState继承的另一个类,而不是使用接口在类中实现AgroState

public class E1_AgroState : AgroState
{
...
}

public class BasicWalkingEnemy : Entity, IAgro
{
    public E1_AgroState agroState { get; }

}
例如,这是我在Swift中使用协议时经常做的事情,但C#编译器会抱怨

“BasicWalkingeMy”未实现接口成员“IAgro.agroState”BasicWalkingeMy.agroState'无法实现'IAgro.agroState',因为它没有匹配的返回类型'agroState'。[组件CSharp]CSharp(CS0738)

目前,我发现的一个解决方案是:

public class BasicWalkingEnemy : Entity, IAgro
{
    public AgroState agroState { get { return _agroState; } }
    public E1_AgroState _agroState { get; private set; }
}
但我认为这是非常不雅的。
我的问题有更好的解决方案吗?

通常,您可以通过显式接口实现来实现,这样,任何只通过
IAgro
了解您的对象的人都可以看到
AgroState
,但任何知道它正在使用
basicwalkingemy
的代码都会得到
E1\u AgroState

//注意:属性名称已更改,以符合.NET命名约定
公共类基本会话:实体,IAgro
{
//显式接口实现
AgroState IAgro.AgroState=>AgroState;
//常规属性
公共E1_AgroState AgroState{get;私有集;}
}

我一直想知道显式实现有什么好处(我的意思是除了在不同接口中使用相同的名称外),只是学到了一些非常有用的东西,非常感谢!