接口不能声明类型 [英] Interfaces cannot declare types

查看:208
本文介绍了接口不能声明类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所使用的另一种装配方法的API中的抽象类。班里有它里面定义了一个嵌套枚举,有点像这样:

I have an abstract class in an API that is used by methods in another assembly. The class has a nested enum defined inside it, a bit like this:

abstract public class Thing
{
    public enum Status { Accepted, Denied, Pending };
    abstract public Status status { get; private set; }
    etc...
}



然后我决定这将是一个更好的设计,如果事情是一个接口。但我不能做到这一点:

I then decided it would be a better design if Thing was an interface. But I can't do this:

public interface Thing
{
    enum Status { Accepted, Denied, Pending };
    Status status { get; }
    etc...
}

这产生错误信息界面不能声明类型。但是,如果我移动枚举的定义的接口外,首先我会打破封装(状态类型真正属于的,并且是毫无意义的自身),更重要的是我会去和修改代码使用此的许多其他组件。你能想到的任何解决方案?

This produces the error message "Interfaces cannot declare types." However, if I move the definition of the enum outside of the interface, firstly I'd be breaking encapsulation (the Status type really belongs to Thing and is meaningless on its own) and more importantly I would have to go and modify the code in the many other assemblies that use this. Can you think of any solutions?

推荐答案

由于错误指示,你只需要拉状态接口之外。据我所知,它打破封装,但真的没有办法解决这个。我建议你​​换状态的名称的东西表明一个强大的关系的事情 - ThingStatus 应该做的伎俩

As the error indicates, you just have to pull the definition of Status outside of the interface. I understand that it breaks encapsulation, but there's really no way around this. I suggest you change the name of Status to something which indicates a strong relation to Thing -- ThingStatus should do the trick.

enum ThingStatus { Accepted, Denied, Pending };

public interface Thing
{
    ThingStatus status { get; }
    etc...
}

这篇关于接口不能声明类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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