This question already has answers here:
Java Reflection: get instances of a given class found by entering its name?
                                
                                    (5个答案)
                                
                        
                                5年前关闭。
            
                    
例如,

// Object 1
SensorMeasurement tempSDSMInfo1 = new SensorMeasurement("temperatureMeasurement", tempSDStruct, "SensorDriver", "TemperatureSensor");

// Object 2
SensorMeasurement tempSDSMInfo2 = new SensorMeasurement("temperatureMeasurement", tempSDStruct, "SensorDriver", "TemperatureSensor");

//Object 3
SensorMeasurement tempSDSMInfo3 = new SensorMeasurement("temperatureMeasurement", tempSDStruct, "SensorDriver", "TemperatureSensor");


现在在程序后面,我想检索特定类的所有实例化对象吗?

我不想将它们存储在集合中。还有其他方法吗?

最佳答案

确实没有其他方法。解决此问题的最佳实践方法是将工厂用于SensorMeasurement,或使用管理器类。然后,您以后便可以通过工厂或经理来引用它们,并在工厂或经理中执行操作,还可以建立一个集中的逻辑中心。

09-26 05:35