访问泛型类的静态方法
我想访问类上的静态方法,但让该类传入泛型。
我已经做了以下操作:
class Base{
public static String getStaticName(){
return "Base";
}
}
class Child extends Base{
public static String getStaticName(){
return "Child";
}
}
class StaticAccessor{
public static <T extends Base>String getName(Class<T> clazz){
return T.getStaticName();
}
}
StaticAccessor.getName() // --> "Base"
这将返回“Base”,但我想要的是“Child”,任何人都可以提出没有反思的建议吗?
I'd like to access a static method on a class, but have that class passed in a generic.
I've done the following:
class Base{
public static String getStaticName(){
return "Base";
}
}
class Child extends Base{
public static String getStaticName(){
return "Child";
}
}
class StaticAccessor{
public static <T extends Base>String getName(Class<T> clazz){
return T.getStaticName();
}
}
StaticAccessor.getName() // --> "Base"
This will return "Base" but what i'd like is "Child" anybody a suggestion without reflections?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有反射,您就无法做到这一点,因为类型
T
在运行时会被擦除(这意味着它将减少到其下限,即Base
)。由于您确实可以访问
Class
,因此您可以通过反射来做到这一点,但是:请注意,我认为此类代码是代码味道,并且它是相当脆弱。您能告诉我们为什么您需要它吗?
You can't do that without reflection, because the type
T
is erased at runtime (meaning it will be reduced to its lower bound, which isBase
).Since you do have access to a
Class<T>
you can do it with reflection, however:Note that I'd consider such code to be code smell and that it is pretty fragile. Could you tell us why you need that?
如果您可以在静态访问器中传递对象实例而不是类,那么,有一个简单而优雅的解决方案:
输出为:
If it is OK for you to pass an object instance rather than Class in your static accessor, then, there is a simple and elegant solution:
The output is:
我不相信你不经过反思就能做到这一点。
看来您应该做的不是使用静态方法。您正在使用继承,但静态方法不遵循继承规则。
I don't believe you can do this without reflection.
It appears you should be doing is not using static methods. You are using inheritance but static methods do not follow inheritance rules.