0

我正在尝试遍历我创建的对象并使用 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!");
        }
    }
}
4

2 回答 2

0

您可以检查对象是哪个类并将其转换为子类以访问方法,这是您想要实现的吗?

    DataObject currentDataObject = it.next();
if(currentDataObject instanceof DoubleDataObject){
 DoubleDataObject doubleData = (DoubleDataObject) currentDataObject;
//check your methods
}
于 2013-10-21T08:57:34.803 回答
0

你可以使用这个:

if (currentDataObject.getClass().equals(DoubleDataObject.class)) {
DoubleDataObject currentDoubleDataObject = (DoubleDataObject)currentDataObject;
currentDoubleDataObject .setValue(1.4d);
}

测试 currentDataObject 是否是 DoubleDataObject (而不是继承自 DoubleDataObject 的类)

于 2013-10-21T08:59:48.390 回答