[英]PropertyChangeListener and polymorphism inconsistencies (with packages)
When the following classes are separated into different packages it null-pointers: java.lang.NullPointerException: Cannot invoke method addPropertyChangeListener() on null object.
将以下类分为不同的包时,它会使用null指针:
java.lang.NullPointerException: Cannot invoke method addPropertyChangeListener() on null object.
Combining them into a single package, however, works. 但是,将它们组合成一个软件包是可行的。
Package parents
: 套餐
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 {}
Package children
包
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"
}
}
Put them all together into one package/file (commenting out import parents.*
) and you'll find it works, printing: 将它们全部放到一个包/文件中(注释
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]
Why is that? 这是为什么?
将通用<Parent>
添加到Parent类声明使事情再次运行:
public class Parent extends GrandParent <Parent> { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.