简体   繁体   English

如何获取在类中确定的对象的实例

[英]How can I get the instance of an object that is determined in the class

I have a class that has structure containing other classes, each of these classes also may contain different classes. 我有一个具有包含其他类的结构的类,每个这些类也可能包含不同的类。 So this is tree structure of classes. 这就是类的树结构。

public class FirstLevelClass {
   public SecondLevelClass1 secondLevelClass1;
   public SecondLevelClass2 secondLevelClass2;
   ...
}

public class SecondLevelClass1 {
   public ThirdLevelClass thirdLevelClass;
   public String fieldSecondLevelClass;
   ...
}

I have initialized variable of FirstLevelClass. 我已经初始化了FirstLevelClass的变量。

FirstLevelClass firstLevelClass;

I need to get instance of secondLevelClass1 that is stored inside firstLevelClass. 我需要获取存储在firstLevelClass中的secondLevelClass1的实例。 How can I do that having a name of the class SecondLevelClass1? 我该如何命名为SecondLevelClass1类?

Basically I need to get value of the field inside SecondLevelClass1. 基本上,我需要获取SecondLevelClass1内部字段的值。 But I failed when wrote this: 但是我写这篇文章的时候失败了:

String fieldName = "fieldSecondLevelClass";
Field[] fields = firstLevelClass.getClass().getDeclaredFields();
Object foundValue = null;

for (Field field : fields) {
   Field foundField = null;
   foundField = field.getType().getDeclaredField(fieldName);
   foundValue = fieldSecondLevelClass.get((Class.forName(foundField.getDeclaringClass().getName())).newInstance());
}

Because the instance that I call fieldSecondLevelClass.get(...) with isnt initialized. 因为我用isnt初始化的fieldSecondLevelClass.get(...)实例已初始化。 But if i write 但是如果我写

foundValue = foundField.get(((FirstLevelClass) firstLevelClass).secondLevelClass1);

it will work fine. 它将正常工作。 So, do any of you ideas how can I pass right instance of secondLevelClass1 using the name of it and instance of FirstLevelClass to fieldSecondLevelClass.get(...)? 因此,您有什么想法要如何使用它的名称和FirstLevelClass的实例将secondLevelClass1的正确实例传递给fieldSecondLevelClass.get(...)?

It is very difficult to understand what you are trying to do, but I will attempt to simplify this for you. 很难理解您要做什么,但是我将尝试为您简化此过程。

First, your fields are declared public. 首先,您的字段被宣布为公开。 And even if they were not, you could expose getters/accessors, so there is no reason for reflections. 即使它们不是,您也可以公开获取器/访问器,因此没有理由进行反思。

It's also a red flag to me that you have a class for first level, another class for second level, and a third class for third level. 对我来说,这也是一个危险信号,即您有一个针对第一级的课程,另一个针对第二级的课程,以及一个针对第三级的课程。 I suppose it depends on how much these types vary in their interface. 我想这取决于这些类型在它们的界面中有多少不同。 If this tree needs to go any deeper than 3 levels, I suggest abstracting these out into a generic Node class which has references to its children and you can nest them in any order. 如果此树需要深入到3个以上的层次,建议将其抽象为一个通用Node类,该类具有对其子级的引用,您可以按任何顺序嵌套它们。

public class Node {
    private List<Node> children = new ArrayList<>();

    public void addChild(Node c) {
       children.add(c);
    }

    public Node[] getChildren() {
        return children.toArray(new Node[0]);
    }
}

public class SomethingElse {
    public static void main(String[] args) {
        Node firstLevel = new Node();
        Node secondLevel = new Node();
        secondLevel.addChild(new Node());
        firstLevel.addChild(secondLevel);
    }
}

This would be much easier to work with. 这将更容易使用。 However, the design cost is that it forces all nodes at all tree depths to have identical behavior. 但是,设计成本是它迫使所有树深度的所有节点具有相同的行为。 If that is not desired, then this may not work well for you. 如果不希望这样做,那么这可能对您而言效果不佳。

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

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