【发布时间】:2016-01-12 01:27:05
【问题描述】:
有没有办法使用非静态内部类名作为类型参数? 类似的东西:
public class Foo {
...
// non-static inner class
public class Bar {
...
}
}
在某处
public Baz<Bar> obj;
如果 Bar 类是静态的,我可以编写
public Baz<Foo.Bar> obj;
但在这种情况下,Foo 中的非静态变量不能在 Bar 中使用。问题是我需要在 Bar 中使用 Foo 的非静态成员。
基于 Java Play 框架的 Web 应用中的真实情况:
public class SignupController extends Controller {
@Inject
private UserService userService;
...
public class SignupForm {
public String validate() {
if (userService.findUserByEmail(email) != null) {
return Messages.get("error.email.is.in.use");
}
return null;
}
}
}
在视图模板中
@(signupForm: Form[controllers.SignupController.SignupForm])
...
userService 必须在内部类 SignupForm 中可见,并且不能是静态的。这就是为什么 SignupForm 也不能是静态的。
很遗憾,这不会被编译。
是否可以将 SignupForm 用作 Form 类的类型参数?
提前致谢。
【问题讨论】:
-
unfortunately this will not be compiled.你尝试编译了吗? -
好吧,我太着急了,我对这种情况的“概括”是不正确的。这就是为什么它“掩盖”了真正的问题(问题的第二部分)。
标签: java generics playframework playframework-2.0