T具有不兼容的边界 - Java泛型

时间:2017-05-23 17:58:18

标签: java generics playing-cards

我试图通过制作一个类似于此的通用CardDeck类来学习泛型;

Card.java

package com.tn.deck;

public class Card<S extends Comparable<S>, R extends Comparable<R>> implements Comparable<Card<S, R>> {
    private S suit;
    private R rank;

    public Card(S suit, R rank) {
        this.suit = suit;
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "Card{" +
                "suit=" + suit +
                ", rank=" + rank +
                '}';
    }

    @Override
    public int compareTo(Card<S, R> o) {
        return rank.compareTo(o.rank);
    }
}

Deck.java

package com.tn.deck;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;


public final class Deck<T extends Card> {
    private List<T> deck;

    public <S extends Comparable<S>, R extends Comparable<R>> Deck(int numberOfDecks, S[] suits, R[] ranks) {
        this.deck = initializeDecks(numberOfDecks, suits, ranks);
        shuffle();
    }


    private <S extends Comparable<S>, R extends Comparable<R>> List<T> initializeDecks(S[] suits, R[] ranks) {
        return Arrays.stream(suits)
                .flatMap(suit -> Arrays.stream(ranks).map(rank -> new Card<>(suit, rank)))
                .collect(Collectors.toList()); // === It complains here. ===
    }

    private <S extends Comparable<S>, R extends Comparable<R>> List<T> initializeDecks(int numberOfDecks, S[] suits, R[] ranks) {
        return IntStream.range(0, numberOfDecks)
                .mapToObj(i -> initializeDecks(suits, ranks))
                .flatMap(Collection::stream)
                .collect(Collectors.toList()); 
    }

    public void shuffle() {
        Collections.shuffle(deck);
    }

    @Override
    public String toString() {
        return "Deck{" +
                "deck=" + deck +
                '}';
    }
}

我得到的错误是

Error:(57, 25) java: incompatible types: inference variable T has incompatible bounds
    equality constraints: T
    lower bounds: com.tn.deck.Card<S,R>

可能只是我不理解泛型,但我真的很感激有人向我解释我做错了什么。

3 个答案:

答案 0 :(得分:1)

这是因为您正在创建Card,但必须返回List<T>T可以是Card的任何子类型,而不是Card本身。

当您使用泛型类并且需要创建T的新实例(或任何已擦除的泛型类型)时,您应该接受能够执行此操作的工厂。例如:

public <S extends Comparable<S>, R extends Comparable<R>> Deck(int numberOfDecks, S[] suits, R[] ranks, BiFunction<S, R, T> cardFactory) {
    this.deck = initializeDecks(numberOfDecks, suits, ranks, cardFactory);
    shuffle();
}


private <S extends Comparable<S>, R extends Comparable<R>> List<T> initializeDecks(S[] suits, R[] ranks, BiFunction<S, R, T> cardFactory) {
    return Arrays.stream(suits)
            .flatMap(suit -> Arrays.stream(ranks).map(rank -> cardFactory.apply(suit, rank)))
            .collect(Collectors.toList());
}

private <S extends Comparable<S>, R extends Comparable<R>> List<T> initializeDecks(int numberOfDecks, S[] suits, R[] ranks, BiFunction<S, R, T> cardFactory) {
    return IntStream.range(0, numberOfDecks)
            .mapToObj(i -> initializeDecks(suits, ranks, cardFactory))
            .flatMap(Collection::stream)
            .collect(Collectors.toList());
}

答案 1 :(得分:1)

为什么会出现此错误?泛型参数T extends CardCard的所有子类,包括它自己在这里是允许的。当泛型参数TCard的子类类型时,错误发生在您的代码initializeDecks尝试返回超类实例而不进行强制转换的情况下。让我们看一个简单的具体例子:

class Superclass{}
class Subclass extends Superclass{}

Subclass sub= ...;
// you can assign subclass's instance to superclass since it is-a superclass
Superclass sup = sub;
// you can assign superclass's instance to subclass you need casting it down
Subclass sub2 = (Superclass) sup;

您可以按以下方式修改代码:

<S extends Comparable<S>, R extends Comparable<R>> 
        List<? super T> initializeDecks(...){ 
  ....
}

答案 2 :(得分:1)

在获取错误的方法中,您创建了一堆Card<S, R>个对象,然后尝试将它们放入List。为了使其工作,List需要能够保存该类型的对象。

当且仅当对象的类是List<T>或子类T时,T才能保存对象。因此,要使此行有效,Card<S, R>必须是T的子类。现在,如果您查看T上的约束,您指定的是TCard的子类。

这个约束并不能保证将卡片放在列表中所需的关系,所以你会收到错误。