1

我是编程新手,如果它看起来很糟糕,请原谅我。我不太确定我在做什么。

当我编译我的代码时,我收到以下错误:

error: constructor Card in class Card cannot be applied to given types;
required: no arguments
found: String, String, int
reason: actual and formal arguments differ in length

这是我的代码:

public void testConvenienceConstructor() {
    System.out.println("Card...");
    Card instance = new Card("4", DIAMONDS, 4);
    assertEquals("4", instance.getName());
    assertEquals(DIAMONDS, instance.getSuit());
    assertEquals(4, instance.getValue());

这是我的 Card 类代码:

package model;

public class Card implements CribbageConstants {
//-----fields-----
private String name;     //the name of the card
private String suit;     //the suit of the card
private int value;       //the value of the card

//---------- Constructors ---------
/**
 * No argument constructor - set default values for card
 */
public Card() {

        name = "ACE";
        suit = "CLUBS";
        value = 1;
    }
//-------------- Utility methods --------------

    /**
     * Provide a text representation of a card.
     *
 * @return The banana's name, suit, and value
 */
public String getName() {
    return name;
}

public String getSuit() {
    return suit;
}

public int getValue() {
    return value;
}

//------mutator-----
public void setName(String name) {
    this.name = name;
}

public void setSuit(String suit) {
    this.suit = suit;
}

public void setValue(int value) {
    this.value = value;
}
//-----------utility methods------------
}
4

3 回答 3

1

使用new Card("4", DIAMONDS, 4);,您正在调用一个构造函数,Card它需要 a String、 aString和 an int。但是不存在这样的构造函数!所以这就是编译器不高兴的原因。

将此添加到您的代码中:

public Card(String name, String suit, int value) {
    this.name = name;
    this.suit = suit ;
    this.value = value;
}
于 2013-09-29T06:12:48.017 回答
1

您正在尝试在这里构建一张卡片:

Card instance = new Card("4", DIAMONDS, 4);

使用不存在的构造函数。

在类中Card,您需要创建一个接受给定类型的构造函数:

public Card(String nme, String suit, int val) {

        name = nme;
        suit = suit;
        value = val;
}

您还必须DIAMONDS作为我相信的字符串发送(用双引号括起来):

Card instance = new Card("4", "DIAMONDS", 4);

如果您不想添加另一个构造函数,您可以更改启动代码:

Card instance = new Card();
instance.setName("4");
instance.setSuit("DIAMONDS");
instance.setValue(4);
于 2013-09-29T06:12:59.167 回答
1

将此添加到您的课程中:

public Card(String name, String suit, int value)
{
    this.name = name;
    this.suit = suit;
    this.value = value;
}
于 2013-09-29T06:14:18.070 回答