简体   繁体   English

如何在Java中组合两个对象?

[英]How do I combine two objects in Java?

Consider: 考虑:

public class test01{

    public void doSomething(){
        // do something
    }

}

public class test02{

    public void printSomething(){
        // print something
    }

} 

// in main
test01 t1 = new test01();
test02 t2 = new test02();

// I want do to something like this
test01 t3 = t1.merge(t2);

// with t3 I should be able to access both t1 and t2 functions
t3.doSomething();
t3.printSomething();

Please let me know if this is possible in Java? 如果可以在Java中使用,请告诉我? If yes, then let me know how can I achieve this? 如果是,那么让我知道如何实现这一目标?

There is no multiple inheritance in java. java中没有多重继承。 What you can do is making test02 a subclass of test01 then create test03 as a subclass of test02 . 你可以做的是使test02的子类test01然后创建test03作为的子类test02

OR 要么

you can compose them into a Test03 class like this: 你可以把它们组成一个像这样的Test03类:

public class Test03 {

    private Test01 test01;
    private Test02 test02;

    public void doSomething() {
        test01.doSomething();
    }

    public void printSomething() {
        test02.printSomething();
    }

}

Please note that in java you shouldn't use class names like test01 . 请注意 ,在java中,您不应该使用像test01这样的类名。 They should be meaningful and comform to the java class naming guidelines. 它们应该是有意义的并且符合java类命名准则。

Your best option is probably this: 你最好的选择可能就是:

public class TestSet {
    private test01 t1 = new test01();
    private test02 t2 = new test02();

    public void doSomething() {
        t1.doSomething();
    }

    public void printSomething() {
        t2.printSomething();
    }
}

In some languages, multiple inheritance is supported, which may be what you're looking for here. 在某些语言中,支持多重继承,这可能是您在这里寻找的。 But not in Java. 但不是用Java。 You may or may not want to make a couple of interfaces here to tie TestSet more closely together with test01 and test02. 您可能想要也可能不想在这里创建几个接口,以便将TestSet与test01和test02更紧密地结合在一起。

Please let me know if this is possible in Java? 如果可以在Java中使用,请告诉我?

It is not possible. 这不可能。 You cannot combine behaviour dynamically like that in Java. 您无法像Java中那样动态组合行为。

The normal way to combine behaviour in Java is to use some combination of inheritance and wrappering or delegation. 在Java中组合行为的常规方法是使用继承和包装或委派的某种组合。 Even then, there will be an issue of subtyping ... unless you use interfaces ... because Java does not allow a class to have multiple (direct) superclasses. 即使这样,也会出现子类型的问题......除非你使用接口......因为Java不允许类有多个(直接)超类。

Consider for @Panzercrisis's example. 考虑@ Panzercrisis的例子。 While his test03 class implements methods with the same signatures as the test01 and test02 classes, an instance of test03 is not type compatible with either of them. 虽然他的test03类实现具有相同签名的方法test01test02类的一个实例test03不是类型与其中任何兼容。 (You can't use a test03 instance as a test01 or a test02 . Java doesn't support duck typing!) (您不能将test03实例用作test01test02不支持duck typing!)

To address that you would need to define interfaces face01 and face02 that are implemented by test01 and test02 respectively. 为了解决这个问题,您需要分别定义由test01test02实现的接口face01face02 Then you would implement test03 as implementing both face01 and face02 . 然后,你将实现test03为实现 face01face02

But this is all static classes and static typing. 但这是所有静态类和静态类型。


Under some circumstances, you could use DynamicProxy or something similar to "synthesize" a class that "merges" the behaviour of two existing classes. 在某些情况下,您可以使用DynamicProxy或类似的东西来“合成”一个“合并”两个现有类的行为的类。 However, that is all done with static types and code generation behind the scenes. 但是,这些都是在幕后使用静态类型和代码生成完成的。 Moreover, this approach would only viable if you'd had the foresight to define a bunch of interfaces (eg face01 and face02 ) and write your application code against the interfaces rather than the implementation classes. 而且,这种方法只有在你有远见来定义一堆接口(例如face01face02 )并根据接口而不是实现类编写应用程序代码时才可行。

你可以在另一个类X中将类Y定义为内部类,并且可以在类X中使用类Y.除此之外,我知道没有办法做到这一点。

Short answer, no it's not possible how you describe. 简短的回答,不,你不可能如何描述。

The appearance of doing this might be possible if test01 and test02 where interfaces and you had a third class test03 implement both. 如果test01test02接口和你有第三类test03实现这两者,那么这样做的外观可能是可能的。 In C++ this would be done by multiple inheritance but that would function much the same way (ie you would have to create a third class that instead extends both) and this option isn't available in Java anyway. 在C ++中,这可以通过多重继承来完成,但是它的功能大致相同(即你必须创建第三个类,而不是扩展它们),而且无论如何这个选项在Java中都不可用。

Another option would be some sort of composition such as @Panzercrisis describes. 另一种选择是@Panzercrisis描述的某种组合。

The final option (I can think of) would be to have test02 extend test01 but that alters test02 . 最后的选择(我能想到的)是test02扩展test01但是改变了test02

But generally, no, not possible. 但一般来说,不,不可能。

No you can't, really do this in way how you describe this, you can do 不,你不能,真正按照你描述的方式做到这一点,你可以做到

public class test01 extend test02{ .... 公共类test01扩展test02 {....

} }

so your test1, you can use methods from both classes, but if you can really whant yo play with merging classes together, you can do some abomination like this one: 所以你的test1,你可以使用这两个类中的方法,但是如果你真的想要将这些类合并在一起,你可以像这样做一些令人厌恶的事:

public class MyObject {
    Map<String, Object> objects = new HashMap<String, Object>();
    Map<String, Method> methods = new HashMap<String, Method>();

    public Object execute(String methodAName)
{

    Object object = objects.get(methodAName);
    Method method = methods.get(methodAName);
    if (object==null || method==null)
    {
        throw new RuntimeException("method not found");
    }

    try {
        return method.invoke(object, null);
    } catch (Exception e) {
        throw new RuntimeException(e);

    }
}

    public void merge(Object o) {
        for (Method m : o.getClass().getMethods()) {
            objects.put(m.getName(), o);
            methods.put(m.getName(), m);
        }
    }
}

and when you can use it like that 当你可以这样使用它

 MyObject o = new MyObject();
 o.merge(new test01());
 o.merge(new test02());

 o.execute("printSomething");
 o.execute("doSomething");

but as i said, not recommended 但正如我所说,不推荐

You can do something similar using Java inheritance , But not the same thing. 你可以使用Java inheritance做类似的事情,但不是一回事。 you can use extends here. 你可以在这里使用extends But you have to make sure method overriding not happen else your requirement not match. 但是你必须确保方法覆盖不会发生,否则你的要求不匹配。

Test02 extends Test01

Then 然后

Test03 extends Test02 

Now you can achieve some thing similar. 现在你可以实现类似的东西了。

Test03 test=new Test03();

test.callMethodInTest1();
test.callMethodInTest2(); 

Sample: 样品:

public class Test1 {

public String callMethodInTest1() {
    return "test1";
  }
}

.

public class Test2 extends Test1{

public String callMethodInTest2() {
    return "test2";
  }
}

.

public class Test03 extends Test2 {

public static void main(String[] args){
   Test03 sample=new Test03();
   sample.callMethodInTest1();
   sample.callMethodInTest2();
 }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM