[英]Java Generics to variable
How can a java generics assign to variable and then pass it? java泛型如何将其分配给变量然后传递? I don't want to do this :
我不想这样做 :
MyClass myClass = new MyClass();
if (someCondition) {
myClass.<Foo>getDetails();
} else if (someCondition) {
myClass.<Bar>getDetails();
} ... more conditional objects
How can I achieve this: 我该如何实现:
MyClass myClass = new MyClass();
JavaGeneric jg;
if (someCondition) {
jg = Foo;
} else if (someCondition) {
jg = Bar;
}
myClass.<jg>getDetails();
Is this possible? 这可能吗? I've tried to search the documentation about
java generics
but there's no example like this or how to assign it on a variable, they only have examples of passing it on the method/class (T)
. 我试图搜索有关
java generics
的文档,但是没有这样的示例或如何将其分配给变量,它们仅具有将其传递给method/class (T)
示例。
Update: 更新:
The getDetails()
is on the object
: getDetails()
在object
:
public class MyClass {
<T> void getDetails() {
//call method that uses T...
}
}
To achieve, what you are expecting, you need an interface and class should implement it. 要实现您所期望的,您需要一个接口,并且类应该实现它。
interface JavaGeneric
{
public String getDetails();
}
And, now implement it in classes. 并且,现在在类中实现它。
class Foo implements JavaGeneric
{
public String getDetails()
{
return "Foo";
}
}
And, 和,
class Bar implements JavaGeneric
{
public String getDetails()
{
return "Bar";
}
}
Now, create instance in your if-else
and at the end call getDetails
method 现在,在
if-else
创建实例,并在最后调用getDetails
方法
JavaGeneric jg;
if (someCondition) {
jg = new Foo();
} else if (someCondition) {
jg = new Bar();
}
jg.getDetails ();
==Update== ==更新==
I'm not quite sure, what exactly you are trying to achieve. 我不太确定,您到底想达到什么目标。 But, assuming you need to generalized the return type of
getDetails
method. 但是,假设您需要概括化
getDetails
方法的返回类型。
interface JavaGeneric<T>
{
public T getDetails();
}
And, then while implementing 然后,在实施时
class Foo<T> implements JavaGeneric<T>
{
public T getDetails()
{
// your code
}
}
interface JavaGeneric {
public String getDetails(); //also you can have default methods implementation here.
}
class Foo implements JavaGeneric {
public String getDetails(){
return "Foo Details";
}
}
class Bar implements JavaGeneric {
public String getDetails(){
return "Bar Details";
}
}
// Somewhere in code
JavaGeneric jg;
if (someCondition) { //lets say this is false
jg = Foo;
} else if (someCondition) { // lets say this is true
jg = Bar;
}
jg.getDetails(); //we will get "Bar Details"
Just call 刚打电话
myClass.getDetails()
Due to type erasure there is no difference between 由于类型擦除,两者之间没有区别
myClass.<T>getDetails()
And 和
myClass.<R>getDetails()
It seems that you are declaring getDetails()
as generic only because you want to use the type variable T
in the method body. 似乎仅是因为要在方法主体中使用类型变量
T
才将getDetails()
声明为泛型。 The type variable T
is not part of the method's signature or of its return type. 类型变量
T
不是方法签名或其返回类型的一部分。
This is usually not needed. 通常不需要。 Depending on what your method actually does, maybe you can remove the
<T>
from the method declaration and replace T
with ?
根据您的方法实际执行的操作,也许您可以从方法声明中删除
<T>
并将T
替换为?
everywhere in the message body. 邮件正文中的任何地方。
If you want to pass a type from a variable to a method, use a Class<?>
parameter: 如果要将类型从变量传递给方法,请使用
Class<?>
参数:
public class MyClass {
void getDetails(Class<?> cls) {
//...
}
}
Then you just need an object of type Class<?>
: 然后,您只需要一个
Class<?>
类型的对象:
MyClass myClass = new MyClass();
Class<?> jg;
if (someCondition) {
jg = Foo.class;
} else if (someCondition) {
jg = Bar.class;
}
myClass.getDetails(jg);
Generics only exist at compile time due to type erasure , so it's impossible to pass a generic type parameter from a run-time variable. 由于类型擦除 ,泛型仅在编译时存在,因此无法从运行时变量传递泛型类型参数。 It's not clear what you're actually doing with the type in
getDetails
, but if you can't make it work with a Class<?>
object, you'll need to stick with the code in your first example and have a separate method call for each explicit type. 目前尚不清楚您对
getDetails
的类型实际上在做什么,但是如果您不能使其与Class<?>
对象一起使用,则需要坚持第一个示例中的代码并使用单独的方法调用每种显式类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.