java - 如何修复Java中的 'incompatible types: SomeObject cannot be converted to CAP#1'错误

标签 java oop subclass covariance subclassing

我正在尝试制作一个游戏,为了不编写相同的方法和函数,我在 Java 中使用协方差和通配符,以便能够在以后为其他类型(类似)的游戏重用代码。我当前的问题是我无法将 Piece 添加到我的 ArrayList 中。

我怎样才能做到这一点?

这是我当前在 Linux 上的 Java 版本。 openjdk版本“10.0.2”2018-07-17 OpenJDK运行环境(build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4) OpenJDK 64位服务器虚拟机(内部版本10.0.2+13-Ubuntu-1ubuntu0.18.04.4,混合模式)

我尝试了很多小事情,但没有得出结论。也许我应该尝试类型转换多米诺骨牌?

这些是我目前的类(class):

类(class)甲板:

    import java.util.*;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Collections;

    public class Deck {

      private List<? extends Piece> deck;
      private int taille;

      public Deck(){
        this.deck = new ArrayList<Domino>();

        for (int i = 0; i < 7; i++){
          for (int j = i; j < 7; j++){
            Domino d = new Domino(i,j);
            deck.add(d);
          }
        }

        this.taille = deck.size();
      }

      public List<? extends Piece> getDeck(){
        return this.deck;
      }

      public int tailleActuelle(){
        return this.deck.size();
      }

      public int tailleDepart(){
        return this.taille;
      }

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

      public String toString(){
        return "Deck de Dominos : \nTaille de départ : "+this.tailleDepart()+
               "\nTaille actuelle : "+this.tailleActuelle();
      }

      public void printDominosDeck(){
        for (Domino d : deck){
          System.out.print(d+" ");
        }
      }
    }

类(class)作品:

    public class Piece {
      private boolean revele;

      public Piece(){
        this.revele = false;
      }

      public boolean estRevele(){
        return this.revele;
      }

      public void pieceRevele(){
        if (!this.revele) this.revele = !this.revele;
      }
    }

多米诺骨牌类:

    public class Domino extends Piece  {
    //implements Comparable<Domino>

      private int faceD, faceG;

      public Domino(){
        super();
        this.faceD = 0;
        this.faceG = 0;
      }

      public Domino(int d, int g){
        super();
        this.faceD = d;
        this.faceG = g;
      }

      public int getValeurDroite(){
        return this.faceD;
      }

      public int getValeurGauche(){
        return this.faceG;
      }

      public int sommeDesFaces(){
        return this.faceD+this.faceG;
      }

      public String toString(){
        return "["+this.faceD+" | "+this.faceG+"]";
      }
    }

目前,我有以下错误:

    Deck.java:17: error: incompatible types: Domino cannot be converted to CAP#1
            deck.add(d);
                     ^
      where CAP#1 is a fresh type-variable:
        CAP#1 extends Piece from capture of ? extends Piece
    Deck.java:46: error: incompatible types: CAP#1 cannot be converted to Domino
        for (Domino d : deck){
                        ^
      where CAP#1 is a fresh type-variable:
        CAP#1 extends Piece from capture of ? extends Piece
    Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
    2 errors

我想要实现的是能够正常地将我的多米诺骨牌添加到ArrayList中。稍后可能还会推出其他类型的游戏作品!

谢谢

最佳答案

你看到另一个SO了吗answer

更改?将 Piece 扩展为 Piece 至少可以为我编译:

import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

public class Deck {

  private List<Piece> deck = new ArrayList<Piece>();
  private int taille;

  public Deck(){
    this.deck = new ArrayList<Piece>();

    for (int i = 0; i < 7; i++){
      for (int j = i; j < 7; j++){
        Domino d = new Domino(i,j);
        deck.add(d);
      }
    }

    this.taille = deck.size();
  }

  public List<? extends Piece> getDeck(){
    return this.deck;
  }

  public int tailleActuelle(){
    return this.deck.size();
  }

  public int tailleDepart(){
    return this.taille;
  }

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

  public String toString(){
    return "Deck de Dominos : \nTaille de départ : "+this.tailleDepart()+
           "\nTaille actuelle : "+this.tailleActuelle();
  }

  public void printDominosDeck(){
    for (Piece d : deck){
      System.out.print(d+" ");
    }
  }
}

关于java - 如何修复Java中的 'incompatible types: SomeObject cannot be converted to CAP#1'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53948879/

相关文章:

java - Android 平板电脑和相机之间的 USB 批量传输

java - 请帮助我,为什么在输入负值时使用俄罗斯农民算法时会得到负值

Javascript OO 键值对访问器

javascript - 需要帮助将对象值作为 jQuery 选择器拉入

Java可以子类继承和覆盖嵌套类吗?

java - 在 Java 中总是在构造函数之后执行方法

java - Ant yielding中的Beanshell, "Unable to create javax script engine for beanshell"

oop - 这是否解决了 Liskov 替代方矩形违规问题?

python - 表示子类类似于具有一个对象的父类(super class)的结构的最佳方式

ruby - 当前的 Ruby 方法是通过 super 调用的吗?