Javafx ReadOnlyIntegraterRapper和ReadOnlyIntegraterProperty之间有什么区别?
什么时候用哪个? 如果它调用ReadOnlyIntegraterRapper,那么为什么我们仍然可以更改它的值?下面是一个例子Javafx ReadOnlyIntegraterRapper和ReadOnlyIntegraterProperty之间有什么区别?,javafx,Javafx,什么时候用哪个? 如果它调用ReadOnlyIntegraterRapper,那么为什么我们仍然可以更改它的值?下面是一个例子 import javafx.beans.property.*; public class ReadOnlyCheck{ public static void main(String... args){ ReadOnlyIntegerWrapper idWrapper = new ReadOnlyIntegerWrapper(100);
import javafx.beans.property.*;
public class ReadOnlyCheck{
public static void main(String... args){
ReadOnlyIntegerWrapper idWrapper = new ReadOnlyIntegerWrapper(100);
ReadOnlyIntegerProperty id = idWrapper.getReadOnlyProperty();
System.out.println("idWrapper:" + idWrapper.get());
System.out.println("id:" + id.get());
// Change the value
idWrapper.set(101);
System.out.println("idWrapper:" + idWrapper.get());
System.out.println("id:" + id.get());
}
}
所以我想问他们之间有什么区别
编辑:
如果ReadOnlyIntegraterRapper也可以更改该值,那么SimpleIntegerProperty()有什么用途?为什么引入ReadOnlyIntegraterRapper?ReadOnlyIntegraterProperty是ReadOnlyIntegraterRapper的超类
所以ReadOnlyIntegraterRapper是ReadOnlyIntegraterProperty,它的附加行为是定义只读属性非常方便的类。它创建两个同步的属性。一个属性是只读的,可以传递给外部用户。另一个属性是可读写的,只能在内部使用。SimpleIntegerProperty是一个类属性,允许用户读取和写入值。但是,有时确实希望限制对数据的写入访问。在这种情况下,可以使用
ReadOnlyIntegerPropertyWrapper
。此属性可以修改,但也允许您提供该属性的只读视图。仅从返回类型为ReadOnlyIntegerProperty
的属性方法中检索SimpleIntegerProperty
并不能确保属性不可写,因为该类的用户仍然可以将其强制转换为IntegerProperty
,并使用强制转换的结果来设置属性
考虑一下计数器
类的这两个版本
public class Counter {
private final SimpleIntegerProperty value = new SimpleIntegerProperty();
public void increment() {
value.set(value.get() + 1);
}
public ReadOnlyIntegerProperty valueProperty() {
return value;
}
}
及
与类的第一个版本一起工作,如果使用第二个版本,则会产生一个
ClassCastException
。我从中选择了下面的示例
我们可以看到,readonlyintegerrapper
是读/写属性,但是ReadOnlyIntegerProperty
只是一个读属性。
当我们向idWrapper
属性写入新值时,id
的值也会更新(同步)
因此,为什么我们应该用一个来代替另一个,这本书的作者补充道:
通常,包装器属性用作的私有实例变量
一节课。该类可以在内部更改属性。它的一个
方法返回包装类的只读属性,因此
对于外部世界,相同的属性是只读的
一个小小的例子来说明这一点
class Person{
private ReadOnlyIntegerWrapper wealth = new ReadOnlyIntegerWrapper();
/*We return the read-only property of the wrapper class*/
public ReadOnlyIntegerProperty getWealth() {
return wealth.getReadOnlyProperty();
}
private void setWealth(int newValue){
//here, internally we can write a new value to the wealth property.
}
}
ReadOnlyIntgerProperty
在初始化后不能更改(不包括如何提到没有setter方法),而readonlyintgerrapper
can@specializt为什么包装纸可以?那么SimpleIntegerProperty()的用途是什么?。。。什么?你能请说英语的人帮你翻译吗?@specialit我的意思是如果ReadOnlyIntegraterRapper也可以更改值,那么SimpleIntegerProperty()有什么用?为什么他们引入ReadOnlyIntegraterRapper?但是什么时候使用这个类呢?优点是什么?第一个可以通过@FXML绑定使用,因此在FXML文件和场景图内部,第二个对于需要操纵值并使其对外部类只读可见的内部JavaFX感知代码非常有用,例如可以使用ReadOnlyIntegerProperty。此属性可以修改,
确定吗?怎样?那么这个属性有什么用呢?@没什么对不起,忘了那里的包装器部分
Counter c = new Counter();
c.valueProperty().addListener((a,b, newValue) -> System.out.println(newValue));
c.increment();
((IntegerProperty)c.valueProperty()).set(-5);
c.increment();
@Override
public void start( Stage primaryStage ) throws Exception {
ReadOnlyIntegerWrapper idWrapper = new ReadOnlyIntegerWrapper(100);
ReadOnlyIntegerProperty id = idWrapper.getReadOnlyProperty();
System.out.println("idWrapper.get() = " + idWrapper.get());
System.out.println("id.get() = " + id.get());
idWrapper.set(10);
System.out.println("idWrapper.get() = " + idWrapper.get());
System.out.println("id.get() = " + id.get());
}
class Person{
private ReadOnlyIntegerWrapper wealth = new ReadOnlyIntegerWrapper();
/*We return the read-only property of the wrapper class*/
public ReadOnlyIntegerProperty getWealth() {
return wealth.getReadOnlyProperty();
}
private void setWealth(int newValue){
//here, internally we can write a new value to the wealth property.
}
}