作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的静态方法:
public static <U> U findStuff(String id) {
// how can i get the class of type U here ?
Class classObject = ....;
return classObject.newInstance();
}
public static void main(String[] args) {
MyEntity entity = MyClass.<MyEntity>findStuff("abc");
}
我想知道如何从 U 获取类对象?
目前我必须传递类(class),因为我还不知道路..所以,现在我使用这样的东西:
public static <U> U findStuff(Class<U> classObject, String id) {
return classObject.newInstance();
}
public static void main(String[] args) {
MyEntity entity = MyClass.findStuff(MyEntity.class, "abc");
}
最佳答案
要获取对象的类,请使用object.getClass();
顺便说一下,这是一种与泛型无关的常见方法
关于java - 泛型 : getting parameterizedtype classname from a static method?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6015360/
我是一名优秀的程序员,十分优秀!