开发者

How do I instantiate an object when it's constructor expects enumerations?

I have a class that uses enumerable data types. In it's constructor, it uses two of these. When I try to instantiate an object of this class from another file, I get an error.

Here is part of the code from the class:

public class Card {
public static enum colorType {BLACK, RED};
public static enum suitType {CLUB, DIAMOND, HEART, SPADE };
public static enum rankType {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
        NINE, TEN, J开发者_运维百科ACK, QUEEN, KING };
private boolean faceup = false;
private rankType rank;
private suitType suit;

//*************************************************************
//Card- constructor.  initializes a card and makes it face down
//*************************************************************
public void Card(rankType r, suitType s){
    this.rank = r;
    this.suit = s;
    this.faceup = false;
}

When I try to do this:

Card C1 = new Card(ACE,SPADE);

from another file, I get an error. using rankType.ACE and suitType.SPADE as arguments also gives the same error. I can do Card C1 = new Card(); with no errors, but that would create a card with nothing in it. The exact error I'm getting is:

internal error; cannot instantiate Card.<init> at Card to ()

Obviously there is some trick to using enumerables in this situation. What am I doing wrong?

Fixed. Solutions to my problems are in posts from uthomas, Tieson T., and the accepted answer.


Try to remove the void keyword from the Constructor.


You need to specify where the enum is coming from:

Card C1 = new Card(rankType.ACE, suitType.SPADE);

Also, your enums should not start with a lower case letter. You should also leave out the "type." So do this:

public static enum Color {BLACK, RED};
public static enum Suit {CLUB, DIAMOND, HEART, SPADE };
public static enum Rank {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING };

Then the call to the constructor becomes:

Card C1 = new Card(Rank.ACE, Suit.SPADE);

EDIT: You also need to remove the void from the constructor, as @uthomas noted. Constructors return the type of object in which they are defined. That is why they have to have the same name as the class itself.


As the enum is declared within a class, is a inner type so you must qualify using the outer class:

Card C1 = new Card(Card.rankType.ACE, Card.suitType.SPADE);

Could be better if you define the enums in separate classes so can be referenced (the type) directly and using static import like import static RankType.* you could use:

Card C1 = new Card(ACE, SPADE);

Also remove void from constructor as stated.


When removed the void as uthomas wrote, you need to use the correct enums. rankType and suitType are inner classes of Card, so if you want to use them from outside the class, you need to prefix them by the outer class name:

Card C1 = new Card(Card.rankType.ACE,Card.suitType.SPADE)

As an enum is a class, follow the convention and name it starting with an uppercase letter.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜