How can a java generics assign to variable and then pass it? 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)
.
Update:
The getDetails()
is on the 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
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.
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. The type variable T
is not part of the method's signature or of its return type.
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 ?
everywhere in the message body.
If you want to pass a type from a variable to a method, use a Class<?>
parameter:
public class MyClass {
void getDetails(Class<?> cls) {
//...
}
}
Then you just need an object of type 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.