Java通用的另一种通用 [英] Java Generic of Another generic

查看:143
本文介绍了Java通用的另一种通用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有接口:

 接口标识符< T extends Serializable> {
T getID();
}

以及实现此类的类:

  public class Cat implements Identifable< Long> {
public Long getID(){...};
}

一切正常。至今。现在我想创建GenericDAO,为什么我不能创建它:

  public abstract GenericDAO< T extends Identifable< S>> {
T getByID(S id);
}

我只能声明我的GenericDAO为:

  public abstract GenericDAO< T extends Identifable,S> {
T getById(S id);
}

完整的类:

  public CatDAO扩展了GenericDAO< Cat,Long> {
Cat getById(Long id);
}

但我认为这没用,因为我重复了一些信息。我已经宣称,Cat实现了Identifable< Long>,那么为什么我必须声明GenericDAO< Cat,Long>,而不仅仅是GenericDAO<在Java中,每个泛型类型都必须指定。在Java中,每个泛型类型都必须指定。你可以没有指定任何类型,但你不能没有指定一个。

另外,必须在声明中指定每个泛型类型。如果您想让 class GenericDAO< T extends Identifable< U>> ,则必须为 U 到你的类声明中(因为 U 实际上是一个泛型类型):

  public abstract class GenericDAO< T extends Identifable< U>,U> 

以下部分内容不在话下,但您可能会觉得它有用。



我注意到在 GenericDAO 的定义中,两个泛型类型并不相互关联。这可能不是你想要的。



这里有两个泛型匹配的特殊情况( Long 输入 Cat CatDAO 定义)。考虑做这些声明:

  public class Dog implements Identifiable< Long> 
public class DogDAO扩展了GenericDao< Dog,String>

这会迫使您编写 getById 方法在 DogDAO 方法中:

  Dog getById(String id); 

您的 getId 方法在 Dog 返回一个 Long ,所以你的 getById 方法int DogDAO 必须将字符串 s与 Long s进行比较。这是有效的事情,但这有点违反直觉。对于 DogDAO 有一个 getById 方法,它需要一个 Long 参数因为 Dog s ID实际上是 Long s。


$ b更有意义$ b

如果您想将两种类型绑定在一起,您可以将 GenericDAO 类定义为:

  public abstract class GenericDAO< T extends Identifable< S>,S> 

您仍然需要指定第二个参数,但至少编译器可以帮助您确保类型匹配。


I have interface:

interface Identifable<T extends Serializable> {
      T getID();
}

and class that implement this:

public class Cat implements Identifable<Long> {
       public Long getID(){...};
}

everything works fine. so far. Now I want to create GenericDAO, why I cannot create this?:

public abstract GenericDAO<T extends Identifable<S>> {
    T getByID(S id);
}

I can only declare my GenericDAO as this:

public abstract GenericDAO<T extends Identifable, S> {
  T getById(S id);
}

And complete class:

public CatDAO extends GenericDAO<Cat, Long> {
      Cat getById(Long id);
}

But i think it's useless, because I repeat information. I already declared, that Cat implements Identifable< Long >, so why do I must declare GenericDAO< Cat, Long >, not just GenericDAO< Cat > ?

解决方案

In Java, every generic type must be specified. You can go without specifying any type, but you can't go without specifying just one.

Also, every generic type must be specified in the declaration. If you want to have class GenericDAO<T extends Identifable<U>>, you must add the generic type declaration for U to your class declaration like this (since U is actually a generic type here):

public abstract class GenericDAO<T extends Identifable<U>, U> 

The following is partially off-topic, but you might find it useful.

I've noticed that in your definition of GenericDAO two generic types are not tied to each other. This might not be what you want.

What you have here is a particular case in which the two generics are matching (the Long type in the Cat and CatDAO definitions). Consider having these declarations:

public class Dog implements Identifable<Long>
public class DogDAO extends GenericDao<Dog, String>

This would force you to write the getById method in DogDAO method:

Dog getById(String id);

Your getId method in the Dog returns a Long so your getById method int DogDAO would have to compare Strings to Longs. This is valid thing to do, but it's a bit counter-intuitive. Having a getById method for DogDAO that takes a Long parameter makes more sense, since the Dogs IDs are actually Longs.

If you want to tie the two types together, you can define the GenericDAO class as:

public abstract class GenericDAO<T extends Identifable<S>, S>

You still have to specify the second parameter, but at least the compiler can help you make sure that the types are matching.

这篇关于Java通用的另一种通用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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