我正在尝试遍历我创建的对象并使用 get/set 方法更改其某些属性。当我只有一个对象时,我设法使它工作,但现在我有继承父对象的对象,并且每个对象都有我想要更改的稍微不同的属性。例如 String、Float、Integer、Double 对象。在运行时我不知道会找到哪个对象,因此我希望我的 Android 应用程序能够通过唯一地址找到它,并通过迭代父对象来更改特定继承对象的属性。如果有人可以,请帮助我解决这个问题。先感谢您。
这是父对象类代码:
public class DataObject
{
private byte[] address;
private String type;
private boolean updatedFlag;
private boolean editedFlag;
public DataObject()
{
updatedFlag = false;
editedFlag = false;
}
public void setAddress(byte[] address)
{
this.address = address;
}
public void setType(String type)
{
this.type = type;
}
public void setUpdatedFlag(boolean updatedFlag)
{
this.updatedFlag = updatedFlag;
}
public void setEditedFlag(boolean editedFlag)
{
this.editedFlag = editedFlag;
}
public byte[] getAddress()
{
//return Arrays.toString(address);
return address;
}
public String getType()
{
return type;
}
public boolean getUpdatedFlag()
{
return updatedFlag;
}
public boolean getEditedFlag()
{
return editedFlag;
}
}
这是继承的对象类之一:
public class DoubleDataObject extends DataObject
{
private double value;
private String range;
public DoubleDataObject()
{
}
public void setValue(double value)
{
this.value = value;
}
public void setRange(String range)
{
this.range = range;
}
public double getValue()
{
return value;
}
public String getRange()
{
return range;
}
}
这是遍历对象以更改其属性的代码:
private void setUpValues(byte[] address, byte[] value)
{
byte[] addressByteArray = address;
Iterator<DataObject> it = dataObjects.iterator();
while(it.hasNext())
{
DataObject currentDataObject = it.next();
byte[] dataObjectByteArray = currentDataObject.getAddress();
if(addressByteArray[0] == dataObjectByteArray[0])
{
System.out.println("Found subsystem!");
if(addressByteArray[1] == dataObjectByteArray[1])
{
System.out.println("Found highlevel!");
if(addressByteArray[2] == dataObjectByteArray[2])
{
System.out.println("Found low level!");
System.out.println("Found data object!");
currentDataObject.setValue(value);
currentDataObject.setUpdatedFlag(true);
System.out.println("Data Object Address: " + Arrays.toString(currentDataObject.getAddress()));
System.out.println("Data Object Type: " + currentDataObject.getType());
System.out.println("Data Object Value: " + currentDataObject.getValue());
System.out.println("Data Object Range: " + currentDataObject.getRange());
System.out.println("Data Object Updated Flag: " + currentDataObject.getUpdatedFlag());
System.out.println("Data Object Edited Flag: " + currentDataObject.getEditedFlag());
}
else
{
System.out.println("CANNOT FIND DATA OBJECT!");
}
}
else
{
System.out.println("CANNOT FIND HIGH LEVEL!");
}
}
else
{
System.out.println("CANNOT FIND SUBSYSTEM!");
}
}
}