【发布时间】:2021-10-16 15:32:09
【问题描述】:
这篇文章的目的是找出如何避免使用匿名内部类。
我没有与inner anonymous classes 进行广泛的合作,但我正在尝试简化某人的包examples.introduction.novice.simpler.model; 中的以下代码行
public class Person {
public final String name;
// --- Attribute
public static final Attribute<Person, String> NAME = new SimpleAttribute<Person, String>("name") {
public String getValue(Person person, QueryOptions queryOptions) { return person.name; }
};
}
所以为了打开上面的内容并使其更易于阅读,我重写了它(如果我错了,请纠正我):
public class Person {
public final String name;
public Person(String name) {
this.name = name;
}
// TODO: Simplify
// ----------- Attributes -------------
public static final Attribute<Person, String> NAME = new Name<Person, String>("name") ;
}
当然,我创建了Name 类:
public class Name<O, A> extends SimpleAttribute<O, A> {
public Name(String attributeName) {
super(attributeName);
// TODO Auto-generated constructor stub
}
public String getValue(Person person, QueryOptions queryOptions) { return person.name; }
@Override
public A getValue(O arg0, QueryOptions arg1) {
// TODO Auto-generated method stub
return null;
}
}
但是,我收到一个错误:
Exception in thread "main" java.lang.ExceptionInInitializerError
at examples.introduction.novice.simpler.model.Introduction.main(Introduction.java:17)
Caused by: java.lang.IllegalStateException: Attribute 'name' (class examples.introduction.novice.simpler.model.Name) is invalid, cannot read generic type information from it. Attributes should typically EITHER be declared in code with generic type information as a (possibly anonymous) subclass of one of the provided attribute types, OR you can use a constructor of the attribute which allows the types to be specified manually.
at com.googlecode.cqengine.attribute.support.AbstractAttribute.readGenericObjectType(AbstractAttribute.java:139)
at com.googlecode.cqengine.attribute.support.AbstractAttribute.<init>(AbstractAttribute.java:43)
at com.googlecode.cqengine.attribute.SimpleAttribute.<init>(SimpleAttribute.java:55)
at examples.introduction.novice.simpler.model.Name.<init>(Name.java:11)
at examples.introduction.novice.simpler.model.Person.<clinit>(Person.java:23)
... 1 more
回顾一下:
我试图在以下包中简化以下示例(这是我试图简化的原始工作示例,它有两个类:名为Introduction.java 的主类和名为Person.java 的伴随类):
https://github.com/mrarthurwhite/CQEngineIntroExample/tree/master/src/examples/introduction/novice
在下面的包中尝试简化它(它只是尝试解包Person.java 类:
https://github.com/mrarthurwhite/CQEngineIntroExample/tree/master/src/examples/introduction/novice/simpler/model
正如善良和耐心的读者可能从上面观察到的那样,我正在尝试解压缩 Attribute 的匿名内部类。这是Attribute 类和cqengine 库的链接:
https://github.com/npgall/cqengine/blob/master/code/src/main/java/com/googlecode/cqengine/attribute/Attribute.java
以下是子类SimpleAttribute 的链接,它是Attribute 的后代:
https://github.com/npgall/cqengine/blob/master/code/src/main/java/com/googlecode/cqengine/attribute/SimpleAttribute.java
【问题讨论】:
-
如果它以前有效并且已经很容易阅读,为什么需要更改它(并可能在过程中破坏它)?
-
@QBrute 我正在尝试解压缩匿名内部类,因为有些程序员对它不是很熟悉,包括我自己。如您所见,我不太了解匿名内部类,当我尝试对其进行解压缩时,出现上述错误。
标签: java anonymous-class simplify anonymous-inner-class cqengine