简体   繁体   English

Java传递通用对象

[英]Java passing generic objects

I have different objects(Object A and object B). 我有不同的对象(对象A和对象B)。 But some of the objects' fields are same. 但是某些对象的字段是相同的。 I can't change the object classes( i mean i cant write a implement/extends condition for them ). 我不能更改对象类(我的意思是我不能为他们写一个实现/扩展条件 )。 I want to pass the objects to a method which uses the objects' fields They have same fields. 我想将对象传递给使用对象字段的方法。它们具有相同的字段。 I don't want to overloading. 我不想超载。 Which design is the most suitable for this?. 哪种设计最适合?

A obj1 = new A();
B obj2 = new B();

update(obj1);
update(obj2);

// my function
public <T extends myInterface> void update(T obj)
{
    obj.field+=1;
}

public interface myInterface{
   int field=0;
}

--------------
public class A{
   int field;
   .... // smt else
}
--------------
public class B{
   int field;
   .... // smt else
}

If you have two classes which do not implement a common interface or share a common base class, you can't really pass them to your function. 如果您有两个未实现公共接口或不共享公共基类的类,则无法真正将它们传递给函数。

The fact that they have a common field doesn't matter. 他们拥有共同的领域这一事实并不重要。

You have 3 workarounds, none of which is really good: 您有3个变通办法,但都不是很有效:

  1. Have your function accept Object type, and check its type (A or B) inside using instanceof . 让您的函数接受Object类型,并使用instanceof检查其类型(A或B)。 This is ugly and not recommended as any class can be passed inside, and also your code has to check it's type all the time. 这很丑陋,不建议使用,因为任何类都可以在内部传递,而且您的代码必须始终检查其类型。
  2. Have your function accept Object type, Use reflection to access field with specific name, in this way: 让您的函数接受Object类型,以这种方式使用反射来访问具有特定名称的字段:

    Field field = obj.getClass().getDeclaredField('myfeild'); 字段字段= obj.getClass()。getDeclaredField('myfeild'); Object value = field.get(obj); 对象值= field.get(obj);

This is better in that your code doesn't have to check types, but more unsafe. 这样比较好,因为您的代码不必检查类型,但是更不安全。 Any class can be passed to your function, and there's some dark magic which relies on static field names. 任何类都可以传递给您的函数,并且有一些依赖于静态字段名称的黑魔法。 If field name changes, your code breaks. 如果字段名称更改,您的代码将中断。

  1. Perhaps the best - Implement a wrapper for your objects. 也许是最好的-为对象实现包装。 It will have two constructors, one for class A and one for class B. The wrapper will remember which kind of object resides inside. 它有两个构造函数,一个用于类A,一个用于类B。包装器将记住驻留在里面的对象的类型。 In its getField function if will have a single if statement. 在其getField函数中,if将具有单个if语句。 Have your function accept the wrapper type as the argument. 让您的函数接受包装器类型作为参数。

instanceof can be used indentify class of object. instanceof可用于识别对象的类。 Like this: 像这样:

public <T extends myInterface> void update(Object obj)
    {
        if ( obj instanceof A )
        {
            A a = (A)obj;
            a.field+=1;
        }
        if( obj instanceof B )
        {
            B b = (B)obj;
            b.field+=1;
        }
    }

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

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