从通用对象访问字段变量 [英] Accessing Field Variable from Generic Object

查看:97
本文介绍了从通用对象访问字段变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类, ClassOne ClassTwo ,它们更新公共字段数据即,

  public class ClassOne {
public byte [] data = new byte [ 10];

//线程更新数据

}

  public class ClassTwo {
public byte [] data = new byte [10];

//线程更新数据

}

只有一个类及其相关线程在任何给定时间运行,即应用程序在运行时检测到数据源并使用 ClassOne ClassTwo 。我想在一个名为 ParseMyData 的单独类中解析数据,但是对于设置这个 ParseMyData 可以从 ClassOne ClassTwo 访问 data >。



我正在尝试使用泛型,如下所示:

  public class ParseMyData< T> {

T classOneOrClassTwo;

ParseMyData(T t){
classOneOrClassTwo = t;


public void parseIt(){
//需要从ClassOne或ClassTwo访问数据,如下所示:
classOneOrClassTwo.data; //这显然不起作用
}

所以我的问题是如何访问类 ParseMyData 中的字段 data ?我必须使用反射吗?是反射的唯一和最好的方法来使用?



新的泛型和反射,所以想法和指针非常赞赏。

getData()创建一个接口 DataProvider ,其中返回您的数据字段。
然后在你的类 ParseMyData< T> 中,你将写成 ParseMyData< T extends DataProvider> / p>

  public class ParseMyData< T extends DataProvider> {

T classOneOrClassTwo;

ParseMyData(T t){
classOneOrClassTwo = t;
}

public void parseIt(){
classOneOrClassTwo.getData();
}

或者,您也可以使用您的版本,但请执行 instanceof 首先检查,然后转换为 ClassOne ClassTwo 。但我建议你选择第一个选项。


I have two classes, ClassOne and ClassTwo, that update a public field data i.e.,

public class ClassOne {
    public byte[] data = new byte[10];

    // Thread that updates data

}

and

public class ClassTwo {
    public byte[] data = new byte[10];

    // Thread that updates data

}

Only one class and its associated thread is running at any given time, i.e., the app detects the source of the data at run time and uses either ClassOne or ClassTwo. I want to parse the data in a separate class called ParseMyData, but a little confused as to the best way to set this up so ParseMyData can access data from either ClassOne or ClassTwo.

I'm currently trying to do it using generics, something like:

public class ParseMyData<T> {

    T classOneOrClassTwo;

    ParseMyData(T t) {
        classOneOrClassTwo = t;
    }

    public void parseIt() {
        // Need to access data from either ClassOne or ClassTwo here, something like:
        classOneOrClassTwo.data; // this obviously doesn't work
    }

So my question is how do I access the field data from within the class ParseMyData? Do I have to use reflection? Is reflection the only and best method to use?

New to generics and reflection, so thoughts and pointers greatly appreciated.

解决方案

Create an interface DataProvider with a method getData() which returns your data field. Then in your class ParseMyData<T> you will write instead ParseMyData<T extends DataProvider>.

public class ParseMyData<T extends DataProvider> {

    T classOneOrClassTwo;

    ParseMyData(T t) {
        classOneOrClassTwo = t;
    }

    public void parseIt() {
        classOneOrClassTwo.getData();
    }

Alternatively you might also use your version, but do an instanceof check first and then cast to either ClassOne or ClassTwo. But I'd recommend you to go with the first option.

这篇关于从通用对象访问字段变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆