我想利用在 Java采用通用方法所掌握的类别信息,以可能树立一个榜样。 但是,我看不出如何在没有实际证据的情况下提及这一类。 是否可能?
public class Fancy {
static public <TypeToFind> TypeToFind createInSomeCase() {
// any type of logic with TypeToFind "class" generic will do, I just want to reference it.
// the below code is invalid, I could also declare a variable, but can t always create an instance to get the class
if (TypeToFind.getClass().equals(Something.class)) {
return TypeToFind.getInstance();
}
}
}
......后来我可以这样做:
TheResultIsTheParameter t = Fancy.createInSomeCase();
......
TheResultIsAParameter t = Fancy.createInSomeCase(TheResultIsAParameter.class);
......
TheResultIsAParameter t = Fancy.createInSomeCase(t);
我这样说太复杂了?