如何指定实现类必须包含特定字段?
我想编写一个抽象类(或接口),它要么
- 强制所有实现类提供特定类型和名称的字段(最好是静态的)(如果丢失,可能会抛出编译时错误?),要么
- 自动提供实现类中的此类字段。
一个例子是:
public abstract class A {
abstract int counter;
}
public class B extends A {
public int getCounter() {
return counter;
}
}
在这种情况下,B 的 getCounter()
方法将返回一个(静态或实例)counter
特定于 B
类,而不是从 A
继承的值。在Java中有什么办法可以做到这一点吗?
I would like to write an abstract class (or interface) which either
- Forces all implementing classes to provide a field (ideally static) of a particular type and name (maybe by throwing a compile-time error if it's missing?), or
- Automatically provides such fields in implementing classes.
An example would be:
public abstract class A {
abstract int counter;
}
public class B extends A {
public int getCounter() {
return counter;
}
}
in which case B's getCounter()
method would return a (static or instance) counter
specific to the B
class, and not a value inherited from A
. Is there any way to do this in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
确实没有一个好的方法可以做到这一点,我也不认为应该有。您可以使用抽象方法并在基类中编写算法来完成同样的事情,以便它们利用委托方法。
也许如果您提供更多关于您认为需要这样做的原因的详细信息,我们可以帮助您制定更正确的解决方案。
例如:
There is not really a good way to do that, nor do I believe there should be. You can accomplish the same thing using abstract methods and writing your algorithms in the base class so that they take advantage of the delegated methods.
Perhaps if you provide more details on why you think you need to do this, we can help you craft a more correct solution.
For example:
创建一个 HashMap,以类为键,以计数器为值。
Make a HashMap with the class as key, and the counter as value.
接口的主要目的不是告诉其实现者要做什么。它是告诉其他类它的实现者可以做什么。此外,它不应指定任何实现细节。也就是说,它不应该也不能强加任何变量声明。
如果您想要每个子类的计数器,请在超类中声明一个
Map
,其中 key=子类的Class
,并且 value=计数器值(如建议的那样)Thorbjørn Ravn Andersen 的回答)
An interface's main purpose is not to tell its implementors what to do. It is to tell other classes what its implementors can do. Also, it should not specify any implementation details. That said, it shouldn't and can't impose any variable declarations.
If you want a per-subclass counter, then declare a
Map
in the superclass, with key=subclass'sClass
, and value=the counter value (as suggested byThorbjørn Ravn Andersen's answer)
尝试这里列出的众多工具之一:
http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis #Java
我假设这是在具有多个开发人员的开发环境中。
Try one of the many tools listed here:
http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis#Java
I am assuming this is in a development environment with multiple developers.
正如其他人指出的那样,如果没有大量的额外工作,这在语言中是不可能直接实现的。
我的建议是您遵循 Pace 的建议:不要指定计数器,而是将其指定为接口中的方法。
我建议的唯一区别是您将其重命名为更正确地反映您的意图的名称。例如:
您甚至可以编写一个自动单元测试生成器来检查它是否已正确实现,即使您可以在抽象类中创建静态成员,您也必须这样做。
As others have pointed out, this isn't directly possible in the language without a fair amount of extra work.
My suggestion is that you follow Pace's suggestion: instead of specifying a counter, specify it as a method in an interface.
The only difference I'd suggest is that you rename it to something which more correctly reflects your intention. Something like:
You could even code up an automated unit test generator to check that it's been correctly implemented, something you'd have to do even if you could create a static member in an abstract class.