当前位置: 首页 > 知识库问答 >
问题:

如何修复Java中的“不兼容类型:SomeObject不能转换为Cap#1”错误

终洛华
2023-03-14

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

    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+" ");
        }
      }
    }
    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

我试图实现的目标是能够正常地将Dominos添加到ArrayList中。以后可能还会有其他类型的游戏!

谢谢。

共有1个答案

微生博简
2023-03-14

你看到这另一个所以回答了吗?

更改?将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+" ");
    }
  }
}
 类似资料: