通用与类

发布于 2024-12-28 10:28:32 字数 1172 浏览 3 评论 0 原文

你好,我正在使用反射做一些测试...... 到目前为止,我已经得到了这个:

public class MyConveter implements Converter {
private Class<?> myClass;

public MyConveter(Class<?> myClass) {
    this.myClass = myClass;
}

@Override
public boolean canConvert(Class clazz) {
    return clazz.equals(myClass);
}

@Override
public void marshal(Object arg0, HierarchicalStreamWriter arg1, MarshallingContext arg2) {
    // TODO Auto-generated method stub
}

@Override
public Object unmarshal(HierarchicalStreamReader arg0, UnmarshallingContext arg1) {
    try {
        Object obj = myClass.newInstance();
        Field daoField = myClass.getDeclaredField("id");
        daoField.setAccessible(true);
        daoField.set(obj, Integer.valueOf(5));
        Field daoField2 = myClass.getDeclaredField("value");
        daoField2.setAccessible(true);
        daoField2.set(obj, "proj name");
        return obj;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    return null;
}

}

但我不喜欢结果,有没有办法将其更改为:

public class MyConveter<T> implements Converter;

从而删除构造函数?

Hello I'm doing some tests using reflection...
so far I've got this:

public class MyConveter implements Converter {
private Class<?> myClass;

public MyConveter(Class<?> myClass) {
    this.myClass = myClass;
}

@Override
public boolean canConvert(Class clazz) {
    return clazz.equals(myClass);
}

@Override
public void marshal(Object arg0, HierarchicalStreamWriter arg1, MarshallingContext arg2) {
    // TODO Auto-generated method stub
}

@Override
public Object unmarshal(HierarchicalStreamReader arg0, UnmarshallingContext arg1) {
    try {
        Object obj = myClass.newInstance();
        Field daoField = myClass.getDeclaredField("id");
        daoField.setAccessible(true);
        daoField.set(obj, Integer.valueOf(5));
        Field daoField2 = myClass.getDeclaredField("value");
        daoField2.setAccessible(true);
        daoField2.set(obj, "proj name");
        return obj;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    return null;
}

}

But I don't like the outcome is there a way of changing this to:

public class MyConveter<T> implements Converter;

thus removing the constructor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

給妳壹絲溫柔 2025-01-04 10:28:32

你可以用你想要的类型参数来声明你的类;但是,您确实需要尝试转换的类的实例(因为它在 unmarshal 方法中使用)。编译类后,泛型中的所有类型信息都会被删除,因此运行时无法知道要为哪个类创建新实例。

You can declare your class with a type parameter all you want; you do, however, need an instance of the class you are trying to convert (because it is used in the unmarshal method). All type information from generics is removed after compiling the class so the runtime has no way of knowing what class to create a new instance for.

心房敞 2025-01-04 10:28:32

不,除非您更改呼叫,否则您不能这样做

@Override
public boolean canConvert(Class clazz) {
}

。您的更改

public class MyConveter<T> implements Converter;

将与 api 更改同时进行。

另外,基本上,在使用像此类这样的泛型时,您只是说您没有定义允许的任何特定对象,该对象可以是任何东西(任何对象或其子类),请

阅读Effective java或Angelika Laker FAQ以获取更多详细信息。

No you can't do that unless and until you change

@Override
public boolean canConvert(Class clazz) {
}

Call. Your change to

public class MyConveter<T> implements Converter;

will go hand in hand with the api change.

And plus basically while using generice like this Class you are just saying that you dont have any specific object defined to be allowed which can be anything (any Object or its subclass)

Read Effective java or Angelika Laker FAQ for more detail on this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文