Java泛型参数绑定到任何一个类型的范围 [英] Java generics parameter bounding to any of a range of types

查看:133
本文介绍了Java泛型参数绑定到任何一个类型的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种语法或解决方法将泛型类型参数约束为任何的一系列类型?



我知道你可以将一个类型约束为一系列类型(即 AND 逻辑):

  public class MyClass< T extends Comparable< T> &安培;序列化> {} //合法语法

是否存在 OR 逻辑版本,即类似这样的内容:

  public class MyClass< T extends Comparable< T> |序列化> {} //非法语法

如果没有支持这种语法的语法(我不认为有),是否有一种解决方法或方法是一个很好的模式?



对于某些上下文,一个示例用例可能是:

  / ** @return如果obj具有相同的id,或者obj与id相同,则返回true * / 
public< T扩展了MyClass |串GT; boolean sameAs(T obj){
if(obj instanceof String)return this.id.equals(obj);
if(obj instanceof MyClass)返回this.id.equals(((MyClass)obj).id);
返回false;
}

人们似乎越来越挂上上述方法示例的确切语义。我们来试试这个:

  public class MyWrapper< T extends A | B个{
//我的类可以包装A或B(无关的类)。是的,我可能会使用instanceof
}

编辑: <
我在编译时不知道我可能会得到哪些内容(来自外部代码),所以我想避免为每种类型设置具体的类。另外,我必须将我的课程授予一个调用我的class.method的外部系统,但另一个系统可以为我提供各种类的实例,但是狭义定义和已知变体。 p>

有些人评论过 instanceof 为不纯。那么,一个解决方法是使用工厂方法根据传入对象的类来选择具体的类,但该工厂方法必须使用 instanceof ,所以,只要把 instanceof 移到另一个地方 - 你仍然需要 instanceof



或者这个想法从来就不是一个好主意?

除非所有类型都有非空的联合类型,否则它就没有任何意义。一个他们都实现的接口,或者他们都扩展的基类,在这种情况下,你只需指定联合类型。


Is there a syntax or workaround to constrain a generic type parameter to any of a range of types?

I am aware that you can constrain a type to be all of a range of types (ie AND logic):

public class MyClass<T extends Comparable<T> & Serializable> { } // legal syntax

Is there an OR logic version, ie something like this:

public class MyClass<T extends Comparable<T> | Serializable> { } // illegal syntax

If there isn't a syntax that supports this (I don't think there is), is there a workaround or approach that is a good pattern?

For some context, one example use case might be:

/** @return true if the obj is either has the same id, or if obj is the same as id */
public <T extends MyClass | String> boolean sameAs(T obj) {
    if (obj instanceof String) return this.id.equals(obj);
    if (obj instanceof MyClass) return this.id.equals(((MyClass)obj).id);
    return false;
}

People seem to be getting hung up on the exact semantic of my method example above. Let's try this instead:

public class MyWrapper<T extends A | B> {
    // my class can wrap A or B (unrelated classes). Yes I will probably use instanceof
}

EDITED:
I won't know at compile time which I might get (coming from external code), so I want to avoid having concrete classes for each type. Also, I have to give my class to a foreign system who invokes my class.method, but the other system can give me instances of a variety of classes, but a narrowly defined and known variety.

Some people have commented on instanceof being "impure". Well, one workaround is to use a factory method to pick my concrete class based on the class of the incoming object, but that factory method would have to use instanceof, so you're just moving the instanceof to another place - you still need the instanceof.

Or is this idea just not ever a good one?

解决方案

No. It wouldn't make any sense unless all the types had a non-empty union type, e.g. an interface they all implemented, or a base class they all extended, in which case you just specify the union type.

这篇关于Java泛型参数绑定到任何一个类型的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆