java 如何在java中包装类并保存接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10014680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to wrap class in java and save interface?
提问by user710818
I have class like:
我有这样的课程:
MyClass extends MyAbstractClass implement myInterface1, myInterface2,...
I need create new class with additional fields:
我需要创建带有附加字段的新类:
MyType1 field1;
MyType2 field2;
.......
Seems that correct create new class that will wrap MyClass like:
似乎正确创建将包装 MyClass 的新类,例如:
MyWrapClass {
MyClass myClass=new MyClass(...);
MyType1 field1;
MyType2 field2;
.....
But MyWrapClass used as type myInterface1 or myInterface2!
但是 MyWrapClass 用作类型 myInterface1 或 myInterface2!
So question is: should I declare all methods that are needed for interfaces myInterface1, myInterface2 in MyWrapClass ? Or exists another way? Thanks.
所以问题是:我应该在 MyWrapClass 中声明接口 myInterface1, myInterface2 所需的所有方法吗?或者存在另一种方式?谢谢。
回答by Eugene Retunsky
Basically, there are two ways. First one is to extend the base class:
基本上,有两种方法。第一个是扩展基类:
public class MySubClass extends MyClass {
private MyType1 field1;
private MyType2 field2;
....
The second option is to use composition:
第二种选择是使用组合:
public class MySubClass implements myInterface1, myInterface2 {
private MyClass delegate;
private MyType1 field1;
private MyType2 field2;
// for all methods in myInterface1, myInterface2
public SomeType method1() {
return delegate.method1();
}
...
}
The second option is recommended by many Java Gurus:
许多 Java 专家推荐第二个选项:
Josh Bloch's book Effective Java 2nd Edition
Josh Bloch 的《Effective Java 2nd Edition》一书
- Item 16: Favor composition over inheritance
- Item 17: Design and document for inheritance or else prohibit it
- 第 16 条:优先组合而不是继承
- 第 17 条:设计和文件继承或禁止继承
Good object-oriented design is not about liberally extending existing classes. Your first instinct should be to compose instead.
好的面向对象设计不是自由扩展现有的类。你的第一直觉应该是写作。
See also http://en.wikipedia.org/wiki/Composition_over_inheritance
另见http://en.wikipedia.org/wiki/Composition_over_inheritance
Composition over inheritance can simplify the initial design of Business Domain classes and provide a more stable business domain in the long term. Its advantage over inheritance is a more thorough isolation of interests than can be described by a hierarchy of descendant classes. Additionally, inheritance models are often contrived during the definition of business domain classes in order to make sense of the information in the problem domain and do not necessarily reflect the true relationship of various system objects.
从长远来看,组合优于继承可以简化业务领域类的初始设计,并提供更稳定的业务领域。与继承相比,它的优势在于可以比后代类的层次结构更彻底地隔离利益。此外,继承模型通常是在业务领域类的定义过程中设计出来的,目的是为了理解问题领域中的信息,并不一定反映各种系统对象的真实关系。
P.S.: auto-generating code for composition is supported by some of modern IDEs
PS:一些现代 IDE 支持自动生成组合代码
回答by Bohemian
Don't wrap, just subclass:
不要包装,只是子类:
class MySubClass extends MyClass {
MyType1 field1;
MyType2 field2;
...
回答by Neil
Assuming you can't extend MyClass, then no there is no other way you can have MyWrapClass and use it like MyClass. For example, it has happened to me to have to implement Map in order to wrap an actual map. It has many functions and every one of them must be implemented that you intend to use. There's a little leeway here, in that if you're not planning to pass it to a a function you didn't write, you can add all required methods and implement only the ones you need. Though more often than not that isn't the case.
假设您不能扩展 MyClass,那么没有其他方法可以拥有 MyWrapClass 并像 MyClass 一样使用它。例如,我碰巧必须实现 Map 才能包装实际地图。它有许多功能,并且必须实现您打算使用的每一个功能。这里有一点余地,因为如果您不打算将它传递给您没有编写的函数,您可以添加所有必需的方法并仅实现您需要的方法。虽然通常情况并非如此。
Alternatively, you could write a dummy class which implements all methods of the interfaces calling the wrapper. Then you could override this class with your own implementation, allowing you to recycle a wrapper class without having to implement all the methods each time.
或者,您可以编写一个虚拟类,它实现调用包装器的接口的所有方法。然后你可以用你自己的实现覆盖这个类,允许你回收一个包装类,而不必每次都实现所有的方法。
回答by Zubzub
Sounds like a problem proxy might solve: http://docs.oracle.com/javase/1.5.0/docs/guide/reflection/proxy.html
听起来代理可能会解决问题:http: //docs.oracle.com/javase/1.5.0/docs/guide/reflection/proxy.html
Be warned, it's slow (uses reflection)
请注意,它很慢(使用反射)
回答by hongbo
Assume you can't extend MyClass and you want to be able to use MyWrapClass as type myInterface1 or myInterface2. You can do the following:
假设您不能扩展 MyClass,并且您希望能够使用 MyWrapClass 作为 myInterface1 或 myInterface2 类型。您可以执行以下操作:
MyWrapClass implement myInterface1, myInterface2,...{
MyClass myClass=new MyClass(...);
MyType1 field1;
MyType2 field2;
methodInInterface1 () {
myClass.methodInInterface1 ()
}
....
You delegate all methods implementation in myInterface1 and myInterface2 to MyClass. I believe this is called Composition.
您将 myInterface1 和 myInterface2 中的所有方法实现委托给 MyClass。我相信这被称为组合。