我刚刚注意到属性类 http://developer.android.com/reference/android/util/Property.html .我可以在这里看到一些解释 http://developer.android.com/about/versions/android-4.0.html#api但并不真正了解它的用例。如果有人能指出一些我可以更深入地理解这一点的代码 fragment ,那就太好了。
最佳答案
属性是反射的包装器。
比如你有一个对象
public class A {
private int fieldOfA;
private int fieldTwo;
private int fieldThree;
public void setFieldOfA(int a) {
fieldOfA = a;
}
public int getFieldOfA() {
return fieldOfA;
}
public void setFieldTwo(int a) {
fieldTwo = a;
}
public int getFieldTwo() {
return fieldTwo;
}
public void setFieldThree(int a) {
fieldThree = a;
}
public int getFieldThree() {
return fieldThree;
}
}
如果你需要更新phew字段,你必须在没有属性的更新方法中知道他们所有的名字
private void updateValues(final A a, final int value) {
a.setFieldOfA(value);
a.setFieldTwo(value);
a.setFieldThree(value);
}
使用属性,您只能更新属性。
Property aProperty = Property.of(A.class, int.class, "fieldOfA");
Property bProperty = Property.of(A.class, int.class, "fieldTwo");
Property cProperty = Property.of(A.class, int.class, "fieldThree");
Collection<Property<A, Integer>> properties = new HashSet<>();
properties.add(aProperty);
properties.add(bProperty);
properties.add(cProperty);
updateValues(a, 10, properties);
方法是
private void updateValues(final A a, final int value, final Collection<Property<A, Integer>> properties) {
for (final Property<A, Integer> property : properties) {
property.set(a, value);
}
}
正如 laalto 提到的,属性动画使用类似的机制。
关于android - android中Property类的用例是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22768810/