本帖最后由 黑马-刘心武 于 2012-9-6 23:31 编辑
以下程序是获得一副扑克牌,每一张扑克是一个扑克牌对象- class Pokers{
- String color;
- String num;
- }
- class PokerFactory{
- Pokers[] arrPoker = new Pokers[52];
- public void createPoker(){//创建扑克牌
- int index = 0;
- for(int i=0;i<PokerTools.arrColor.length;i++){
- for(int j=0;j<PokerTools.arrNum.length;j++){
- arrPoker[index] = new Pokers();//为什么要循环一次都初始化次,能不能对整个数组元素一次性初始化?
- arrPoker[index].color=PokerTools.arrColor[i];
- arrPoker[index].num = PokerTools.arrNum[j];
- index++;
- }
- }
- }
- class PokerTools{
- public static String[] arrColor= {"黑桃","红桃","梅花","方片"};
- public static String[] arrNum = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
- }
复制代码 以上是用查表法来实现给扑克对象赋以花色和数字,在类PokerFactory中定义了Pokers类型数组,但是每一次赋值前都得对数组元素先声明一个Pokers对象,
能不能使数组所有元素元素一次性初始化为Pokers对象?
|