Java使用const或static方法定义泛型类param

Java使用const或static方法定义泛型类param,java,generics,Java,Generics,我想改进我的代码,特别是我使用泛型类的方式。 在我的项目中,我有大约30门课,如下所示: GenericEntity<T extends Serializable>{ protected T id; public T getId(){ return id;}; ... } public class A extends GenericEntity<Integer>{ ... } public interface IService<T e

我想改进我的代码,特别是我使用泛型类的方式。 在我的项目中,我有大约30门课,如下所示:

GenericEntity<T extends Serializable>{

    protected T id;
    public T getId(){ return id;};
...
}

public class A extends GenericEntity<Integer>{
    ...
}


public interface IService<T extends GenericEntity, T extends Serializable>{
...
}

public class AService extends IService<A,Integer>{
...
}
通用实体{
受保护的T-id;
public T getId(){return id;};
...
}
公共类A扩展了泛型实体{
...
}
公共接口设备{
...
}
公共类AService扩展了IService{
...
}
我只想指定实体Id的类一次,而不是像那样在GenericEntity和Service中指定一次

public class A extends GenericEntity<getIdType()>{
    public final static Class getIdType(){
        return Integer.class;
    }
}

public class AService extends IService<A,A.getIdType()>{
...
}
公共类A扩展了泛型实体{
公共最终静态类getIdType(){
返回Integer.class;
}
}
公共类AService扩展了IService{
...
}
我知道它不能像那样工作,但我希望有办法做到这一点。 谢谢您的帮助。

而不是:

class GenericEntity<T extends Serializable>{
    protected T id;
    public T getId(){ return id;};
}

// THESE ARE UNNECESSARY as far as I can tell
class A extends GenericEntity<Integer>{ }
class B extends GenericEntity<Long>{ }

// where U matches the generic type of GenericEntity<?>
interface IService<T extends GenericEntity<?>, U extends Serializable>{ }

class AService extends IService<A, Integer>{ }
class BService extends IService<B, Long>{ }
类泛型实体{
受保护的T-id;
public T getId(){return id;};
}
//据我所知,这些是不必要的
类A扩展了GenericEntity{}
类B扩展了GenericeEntity{}
//其中U匹配GenericEntity的泛型类型
接口IService>{
公共T getGenericEntity(可序列化id);
}
//“AService”知道“id”将是一个“整数”
类AService实现IService{
Map entityMap=newhashmap();
void方法(){
GenericEntity实体=this.getGenericEntity(Integer.valueOf(1));
整数i1=entity.getIdFromEntity();
//…做事
}
//即使“AService”知道“id”将是一个“整数”
//“IService”接口将其定义为采用“Serializable”
//所以它必须保留该方法签名。
@重写公共GenericEntity getGenericEntity(可序列化id){
返回entityMap.get(id);
}
}
类BService实现IService{
@重写公共GenericEntity getGenericEntity(可序列化id){return null;}
//…类似于服务。。。
}
这将删除所有多余的
classx扩展GenericEntity

您只需要一个generic
GenericEntity
和一个
接口IService
,它不应该知道
GenericEntity.getId()
的具体类型(您可能不希望知道)。此外,您还应该避免将其具体化,因为它是一个
接口


IService
而言,
id
的类型是
Serializable
,因为
IService为什么要这样做?当您声明
AService
时,您既知道
A
的类,也知道它的ID类型。您是否尝试过类似的方法:
IService
。。。然后让
AService实现IService
?我想这样做,如果有一天我需要使用Long而不是Integer,我将进行大约60次修改。我还有单元测试,需要输入Id谢谢你的帖子。我想知道在您的示例中,服务如何知道实体Id的类型?接口IService@ScorprocS编辑。这回答了你的问题吗?是的,谢谢。所以,为了通过Id获得一个通用实体,我只需要这样做。接口IService@ScorprocS确切地我将编辑代码以包含它。如果这个答案对你有效,那么“接受”将不胜感激。
class GenericEntity<T extends Serializable> {
    protected T id;
    public T getIdFromEntity() { return id; }
}

// 'IService' can/should only know of 'id' as some type that extends 'Serializeable'
// So if something implements 'IService' then everything knows it will have
// a method with the signature 'T getGenericEntity(Serializable id);'
interface IService<T extends GenericEntity<?>> {
    public T getGenericEntity(Serializable id);
}

// 'AService' knows that 'id' will be an 'Integer'
class AService implements IService<GenericEntity<Integer>> {

    Map<Serializable, GenericEntity<Integer>> entityMap = new HashMap<>();

    void someMethod() {
        GenericEntity<Integer> entity = this.getGenericEntity(Integer.valueOf(1));
        Integer i1 = entity.getIdFromEntity();
        // ... do stuff
    }

    // even though 'AService' knows that 'id' will be an 'Integer'
    // the 'IService' interface defines this as taking a 'Serializable'
    // so it must keep that method signature.
    @Override public GenericEntity<Integer> getGenericEntity(Serializable id) {
        return entityMap.get(id);
    }
}

class BService implements IService<GenericEntity<Long>> {
    @Override public GenericEntity<Long> getGenericEntity(Serializable id) { return null; }
    // ... similar to AService ...
}