使用数据成员组合类

时间:2016-12-07 22:41:02

标签: java member

我正在编写一个卡片游戏,我在其中创建了Card,Card7,Card8和Card9类。 Card7-9类都扩展了Card类。然而,我的老师查看了代码并说了一些关于我如何使用数据成员将其全部集成到一个类中的内容。我不太清楚他的意思是什么。有什么建议吗?

这是一个卡号类示例(其他2个都相似):

public class Card9 extends Card{

    private String name;
    private char suit, color;
    private int life;

    public Card9(int x, int y, char suit, char color) {
        super(x,y, suit, color);
        name = new String("Nine");
        life = 9;
    }

    public Card9(int x, int y, int random, char suit, char color) {
        super(x, y, random, suit, color);
        name = new String("Nine");
    }
}

1 个答案:

答案 0 :(得分:2)

如果你有100张卡怎么办?你会创建100个卡类吗?

public class Card {
    private String name;
    .
    .
    .
    .

    public Card(String name, ........) {
        this.name = name; // set the name of the card to its global variable
    }
}


public class Main {
    public static void main(String [] args) {
        // create as many card objects you like
        // in the constructor, send appropriate card values for each card
        Card card8 = new Card("card8", ........);
        Card card9 = new Card("card9", ........);
    }
}