首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何修复‘不兼容类型: SomeObject无法转换为CAP#1’错误

如何修复‘不兼容类型: SomeObject无法转换为CAP#1’错误
EN

Stack Overflow用户
提问于 2018-12-27 09:43:02
回答 1查看 2.4K关注 0票数 1

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

我怎么能这么做?

这是我当前在Linux上的Java版本。openjdk版本"10.0.2“2018-07-17 OpenJDK运行时环境(Build10.0.2+13-Ubuntu0.18.04.4) OpenJDK 64位服务器VM (Build10.0.2+13-Ubuntu0.18.04.4,混合模式)

我尝试了很多小事情,但都没有定论。也许我该试试演多米尼克一家?

以下是我目前的课程:

班级甲板:

代码语言:javascript
运行
AI代码解释
复制
    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+" ");
        }
      }
    }

课堂作品:

代码语言:javascript
运行
AI代码解释
复制
    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;
      }
    }

类Domino:

代码语言:javascript
运行
AI代码解释
复制
    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+"]";
      }
    }

目前,我有以下错误:

代码语言:javascript
运行
AI代码解释
复制
    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中。以后可能会出现在其他类型的游戏中!

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-27 10:47:07

你看到另一个了吗,所以answer

? extends Piece更改为Piece至少对我来说是编译的:

代码语言:javascript
运行
AI代码解释
复制
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+" ");
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53948879

复制
相关文章

相似问题

领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文