Constructor<Void> constructor = Void.class.getDeclaredConstructor();
constructor.setAccessible(true);
Void v = constructor.newInstance();
System.out.println("I have a " + v);
public interface LeavesVisitor<OUT>
{
OUT visit(Leaf1 leaf);
OUT visit(Leaf2 leaf);
}
当你实现你的访问者时,你可以明确地将 OUT 设置为 Void,这样你就知道你的访问者总是返回 null,而不是使用 Object
public class MyVoidVisitor implements LeavesVisitor<Void>
{
Void visit(Leaf1 leaf){
//...do what you want on your leaf
return null;
}
Void visit(Leaf2 leaf){
//...do what you want on your leaf
return null;
}
}