[英]PropertyChangeListener and polymorphism inconsistencies (with packages)
將以下類分為不同的包時,它會使用null指針: java.lang.NullPointerException: Cannot invoke method addPropertyChangeListener() on null object.
但是,將它們組合成一個軟件包是可行的。
套餐parents
:
package parents
import groovy.beans.Bindable
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
@Bindable
class GrandParent<T extends Parent> implements PropertyChangeListener {
String pets
GrandParent() {
this.addPropertyChangeListener(this)
}
@Override
void propertyChange(PropertyChangeEvent evt) {
println "prop change -> $evt"
}
}
class Parent extends GrandParent {}
包children
package children
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import groovy.beans.Bindable
import parents.*
class Child extends Parent {
@Bindable
String brothers
static main(args) {
Child child = new Child()
child.pets = "Leo, Sophie"
child.brothers = "Douglas, Ted"
}
}
將它們全部放到一個包/文件中(注釋import parents.*
),您會發現它起作用,並打印:
> prop change -> java.beans.PropertyChangeEvent[propertyName=pets; oldValue=null; newValue=Leo, Sophie; propagationId=null; source=children.Child@7eceb95b]
> prop change -> java.beans.PropertyChangeEvent[propertyName=brothers; oldValue=null; newValue=Douglas, Ted; propagationId=null; source=children.Child@7eceb95b]
這是為什么?
將通用<Parent>
添加到Parent類聲明使事情再次運行:
public class Parent extends GrandParent <Parent> { ... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.