如何修复 Java 中的 'incompatible types: SomeObject cannot be converted to CAP#1' 错误
How to fix 'incompatible types: SomeObject cannot be converted to CAP#1' error in Java
我正在尝试制作游戏,为了不编写相同的方法和函数,我在 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 位服务器 VM(构建 10.0.2+13-Ubuntu-1ubuntu0.18.04.4,混合模式)
我尝试了很多小东西,没有定论。也许我应该尝试投多米诺骨牌?
这些是我目前拥有的 类 :
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;
}
}
Class 多米诺骨牌:
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。以后可能还会有其他类型的游戏作品!
谢谢
你看到另一个 SO answer 了吗?
将 ? extends 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 中使用了协方差和通配符,以便以后可以重新用于其他代码类型的(类似)游戏。我目前的问题是我无法将 Piece 添加到我的 ArrayList。
我该怎么做?
这是我在 Linux 上的当前 Java 版本。 openjdk 版本“10.0.2” 2018-07-17 OpenJDK 运行时环境 (build 10.0.2+13-Ubuntu-1ubuntu0.18.04.4) OpenJDK 64 位服务器 VM(构建 10.0.2+13-Ubuntu-1ubuntu0.18.04.4,混合模式)
我尝试了很多小东西,没有定论。也许我应该尝试投多米诺骨牌?
这些是我目前拥有的 类 :
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;
}
}
Class 多米诺骨牌:
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。以后可能还会有其他类型的游戏作品!
谢谢
你看到另一个 SO answer 了吗?
将 ? extends 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+" ");
}
}
}