静态和接口 [英] Statics and Interfaces

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

问题描述

我实际上是在发布此信息,以验证我是否技术上"请在此处更正总体.因此,我知道接口不能包含静态成员.但是在极少数情况下,我觉得所有子类的合同中都应使用一种方法 妨碍签订合同...但问题在于,始终需要从子类的使用者中静态调用该方法.

I am really posting this to verify if I'm "technically" correct overall here.  So I know that Interfaces cannot contain static members.  But there are rare occasions for which I feel that a method should be in the contract for all subclasses which impelement that contract...but the problem is that method will always need to be called statically from the consumer of the subclass.

例如,我发布了此消息,并收到了巨大的反响.我从一些更有经验的建筑师和工程师那里得到了很多很好的信息.界面:

Example, I posted this and there was a huge response.  I got a lot of good info here from some more experienced architects & Interfaces:

http://stackoverflow.com/questions/3514550/subclass -needs-to-implement-interface-property-as-static

但是问题仍然存在,或者我想澄清的是:

But the question still remains to me or I guess clarification is this:

我得出的结论是,即使您一次或一次拥有一种您强烈认为应该属于合同一部分的方法或某种其他类型的成员,但必须始终从实现您的子类的任何静态类中对其进行访问界面 基本上,您有点被搞砸了.即使从技术上讲它很适合合同,也不要管它.这听起来不错吗?好的做法?

I have concluded that even though you might once-in-a-while have a method or some other type of member that you strongly feel should be part of the contract but it's got to always be accessed statically from any subclasses implementing your interface, that basically you're kinda screwed.  Just leave it out even though technically it's a strong fit for the contract.  Is this sound thinking?  Good practice?

如果可以帮助的话,我尽量不要使用静态变量,但是显然,对于像会话这样的对象,您将需要一种静态的方式来获取它/检查它.可以通过静态属性或方法来实现.在这种情况下,我强烈认为应该成为 我团队中任何人的合同,这些人希望在一些相关项目中继续进行会议……与该项目不同,但出于模式/一致性的考虑,仍将需要实施我的IAPISession.

I try not to use statics if I can help it but obviously for an object like a session you're going to need a static way to get it / check for it.  Either by a static property or method.  And in this case I strongly feel that SHOULD be part of the contract for anybody on my team who wants to create sessions going forward in some related projects...different from this one but still will be required to implement my IAPISession for pattern/consistency sake.

推荐答案

在该上下文中使用抽象类代替接口是否适合设计?
Would it fit into the design to use an abstract class instead of an interface in that context?


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

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