面向对象的分析与设计
为什么我无法在接口中指定静态类型的方法。
有什么替代方法可以做到这一点吗?
但我应该只使用抽象类的接口。
/
在接口中指定访问说明符是否有问题?
我想在接口中指定事件,并且只能由实现的类访问,因此我希望对该事件进行受保护的访问说明符。
我有某些功能可以使用另一个类中的接口,在这种情况下我可以使用公共访问说明符。
Why I can't able to specify static type of methods in interface.
Is there any alternative to do this??
but I should use only inerface insted of abstract class.
/
Is there any problem in specifing acess specifier in interface?
I want to specify the events in Interface and that should be acessed only by implemented class so i want protected acess specifier to that event.
and I have certain function that can be make use of interface in another class in that case i can use public acess specifier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不知道你用的是什么语言,但我会像用 C# 一样回答。
为什么我无法在接口中指定静态类型的方法。
有什么替代方法可以做到这一点吗?
这是因为您无法覆盖静态方法。
你想达到什么目的?
在 C# 中,接口中的成员始终是公共的。 如果需要其他保护级别,请使用抽象类。 如果您无法从界面访问受保护的事件,它们的用途是什么? 那么它与接口无关(记住,接口不能有任何代码)。 如果您指的是只有实现类才能引发事件,请放心,它们是唯一可以引发事件的类。 事件就是这样构建的——只有类本身才能引发事件。 您不能在类外部引发事件(除非您在类上有一个方法来引发事件)。
I don't know what language you're taking about but I'll answer as if in C#.
Why I can't able to specify static type of methods in interface.
Is there any alternative to do this??
It is because you can't override static methods.
What are you trying to achieve?
Members in interfaces are always public in C#. If you need to have other protection levels, use an abstract class. What would the purpose of protected events be if you can't access them from the interface? It has nothing to do with the interface then (remember, the interface can't have any code). If you are referring to that only implementing classes can raise the events, rest assured that they are the only ones that can raise them. Events are built that way - only the class itself can raise events. You can't raise an event external from the class (except if you have a method on the class, raising the event).
接口仅向公众描述您的实现者。 接口的唯一访问修饰符是接口本身的“internal”,这使得该接口仅对同一程序集的所有成员或通过“InternalsVisibleTo”属性已知的成员可见。
如果只有实现者应该看到某个事件,那么该事件就必须位于某种基类中。
静态方法几乎是过程式编程的残余。 它们确实有其用途(参见例如扩展方法),但实际上与 OOP 没有太多关系。
interfaces only describe your implementator to the public. The only access modifier for an interface is "internal" on the interface itself, which makes it an intertface only visible to all member of the same assembly or the ones made known with the "InternalsVisibleTo" attribute.
If only implementators should see a certain event, then that event would have to be in some kind of base class.
A static method is pretty much a remnant of procedural programming. they do have their use (see e.g. extension methods) but really haven't got much to do with OOP.
我不明白你对第一个问题的意思,但你关于在接口中使用 protected 的第二个问题是直接的“不”。 接口被称为“接口”,因为它们指定了可以与该对象通信的“接口”(即公共方法)。
I'm at loss on what you mean with the first question, but your second question about using protected in interfaces is a straight "no". Interfaces are called "interfaces" because they specify the "interface" (i.e. public methods) in which you can communicate with that object.
我对 OOP 的体验与 Actionscript 3 相关,但我确信它是相对的。
静态方法不能被覆盖,并且当类扩展祖先类时,它们不会被使用。
接口中不使用访问修饰符,因为该接口用于设置 PUBLIC 方法。
在 AS3 中,接口只是一个没有块的方法签名,但包括数据类型。
值得注意的是,getter/setter 方法签名也可以在接口中使用。 这些很有用,如果您只使用 setter,您就创建了对私有封装变量的只读公共访问。
至于接口而不是抽象类,这完全取决于您想要如何实现代码。 通常,接口和抽象类一起使用,以利用继承和多态性(扩展/实现)来实现不同的结果。
My experience with OOP is in relation to Actionscript 3, but I am sure it's relative.
Static methods cannot be overidden and they do not get used when a class extends the ancestor class.
Access modifiers aren't used in the interface because the interface is used to setup PUBLIC methods.
In AS3 an interface is merely a method signature with no block, but including data-types.
It is important to note that getter/setter method signatures can be used in an interface as well. These are useful and if you only use a setter you have created a read-only public access to a private encapsulated variable.
As far as interface instead of an abstract class, it all depends on how you want to implement your code. Often times and interface and an abstract class are used together to achieve different results utilizing inheritance and polymorphism (extends / implements).