Java将泛型类型与Void进行比较
我无法比较java泛型类型是否为Void类型。换句话说,我试图确保我的泛型类型T是否为Void。 我的示例实现:Java将泛型类型与Void进行比较,java,generics,types,void,Java,Generics,Types,Void,我无法比较java泛型类型是否为Void类型。换句话说,我试图确保我的泛型类型T是否为Void。 我的示例实现: public abstract class Request<T>{ private T member; protected void comparing(){ if(T instanceof Void) // this make error "Expression expected" runAnother
public abstract class Request<T>{
private T member;
protected void comparing(){
if(T instanceof Void) // this make error "Expression expected"
runAnotherMethod();
//if I type
if(member instanceof Void) //Incovertible types; cannot cast T to java.lang.Void
runAnotherMethod();
}
protected void runAnotherMethod(){...}
}
public class ParticularRequest extends Request<Void>{
}
公共抽象类请求{
私人T会员;
受保护的空比较(){
if(T instanceof Void)//这会导致错误“Expression expected”
runAnotherMethod();
//如果我打字
if(member instanceof Void)//不可转换类型;无法将T强制转换为java.lang.Void
runAnotherMethod();
}
受保护的void runAnotherMethod(){…}
}
公共类特殊请求扩展请求{
}
我试图通过instanceof
,Class
和Class
,T.Class
和Void.Class
来比较id。
但AndroidStudio在每个尝试过的案例中都会显示错误:(
你能帮我比较一下吗?
谢谢。您可以存储属于类的泛型类型的
private
成员
public abstract class Request<T> {
private T memberOfGenericType;
protected void comparing() {
if (memberOfGenericType instanceof Sometype)
runAnotherMethod();
}
protected void runAnotherMethod() { ... }
public T getMemberOfGenericType() {
return memberOfGenericType;
}
public void setMemberOfGenericType(T value) {
this.memberOfGenericType = value;
}
}
公共抽象类请求{
通用类型的私有T成员;
受保护的空比较(){
if(某个类型的genericType实例的成员)
runAnotherMethod();
}
受保护的void runAnotherMethod(){…}
公共T getMemberOfGenericType()的成员{
返回genericType的成员;
}
public void setMemberOfGenericType(T值){
this.memberOfGenericType=值;
}
}
这样,在运行时,memberOfGenericType
的类型将为Sometype
,您将能够编译if
语句。您还可以使用我添加的getter在运行时验证memberOfGenericType
是否为Sometype
无论如何,作为旁注,我想说的是,没有必要使用泛型类型,如果您不将其用作成员、方法或方法参数的类型,那么您应该重新考虑您的设计。另外,类型
Void
是不可实例化的,因此您将无法为类成员传递有效的实例,这或多或少会使if
语句无用。您不能这样使用t。您需要一些实例进行比较。例如,某些成员或参数:
public abstract class Request<T> {
T member;
protected void comparing(T param){
if(member instanceof Void)
runAnotherMethod();
if(param instanceof Void)
runAnotherMethod();
}
protected void runAnotherMethod(){...}
}
公共抽象类请求{
T成员;
受保护的空比较(T参数){
if(成员实例of Void)
runAnotherMethod();
if(参数instanceof Void)
runAnotherMethod();
}
受保护的void runAnotherMethod(){…}
}
访问参数类的更好方法是使用这样一个事实:虽然泛型类无法访问自己的“类”参数,但其子类却可以访问这些参数:请参阅
如果需要,可以使用Guice'
TypeLiterals
,或者重新嵌入它们的逻辑。因为Java中没有属于Void
类型的对象,所以不能在此处使用instanceof
null
是类型为Void
的成员的唯一值。因此,您可能希望执行以下操作:
if (memberOfGenericType == null)
runAnotherMethod();
关于类型Void
无法创建类型为Void
的对象,因为该类只有一个私有构造函数,并且从不从类内调用它。Void
通常用于以下情况:
- 获取表示声明返回
的方法的返回类型的void
对象类
- 作为占位符类型参数,当不打算使用该类型的字段和变量时
public abstract class Request<T> {
private Class<T> clazz;
// constructor that asks for the class of the generic type
public Request(Class<T> clazz) {
this.clazz = clazz;
}
// helper function involving the class of the generic type.
// in this case we check if the generic type is of class java.lang.Void
protected boolean isVoidRequest(){
return clazz.equals(Void.class);
}
// functionality that depends on the generic type
protected void comparing() {
if (isVoidRequest()) {
runAnotherMethod();
}
}
// ...
}
公共抽象类请求{
私人课堂;
//请求泛型类型的类的构造函数
公开请求(课堂){
this.clazz=clazz;
}
//涉及泛型类型类的helper函数。
//在本例中,我们检查泛型类型是否为java.lang.Void类
受保护的布尔值isVoidRequest(){
返回clazz.equals(Void.class);
}
//依赖于泛型类型的功能
受保护的空比较(){
if(isVoidRequest()){
runAnotherMethod();
}
}
// ...
}
当您创建子类时,必须将泛型类型的类传递给超级构造函数
public class LongRequest extends Request<Long> {
public LongRequest() {
super(Long.class);
}
}
public class VoidRequest extends Request<Void> {
public VoidRequest() {
super(Void.class);
}
}
公共类LongRequest扩展请求{
公共请求(){
超级(长类);
}
}
公共类VoidRequest扩展了请求{
公开请求(){
超级(无效类);
}
}
在运行时T
被编译为对象
,实际的类未知。正如其他人所说,您应该维护参数化类型的实例,但这不是自动的:您需要实例化它,并且构造函数T()
不能使用
另外,java.lang.Void
不能被实例化,所以您应该使用另一个类,比如自制的Void
类
试着这样做:
public final class Void {}; // cannot use java.lang.Void, so create another class...
public abstract class Request<T> {
protected abstract T member(); // A member would need to be initialized...
protected void comparing(T param){
if(member() instanceof Void) // Use our Void not java.lang.Void
runAnotherMethod();
}
protected void runAnotherMethod(){...}
}
public class ParticularRequest extends Request<Void>{
@Override
protected Void member() { return new Void(); } // Could be optimized...
}
public final class Void{};//无法使用java.lang.Void,请创建另一个类。。。
公共抽象类请求{
受保护的抽象T成员();//需要初始化成员。。。
受保护的空比较(T参数){
if(member()instanceof Void)//使用我们的Void而不是java.lang.Void
runAnotherMethod();
}
受保护的void runAnotherMethod(){…}
}
公共类特殊请求扩展请求{
@凌驾
受保护的Void成员(){return new Void();}//可能已优化。。。
}
编辑: 我不明白,但是你为什么需要这个。 如果不同类型有不同的子级,那么也可以有不同的实现 Som
public abstract class Request<T> {
protected abstract T method();
}
public class RequestInt extends Request<Integer> {
@Override
protected Integer method() {...}
}
public class RequestText extends Request<String> {
@Override
protected String method() {...}
}