class Test7
{
public static void main(String[] args)
{
String[] cs={"黑桃","红桃","方块","梅花"};
String[] ns={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
Puker[] ps=new Puker[cs.length*ns.length]; //可以装入52个扑克对象 ps中的元素现在的值是null
int index=0;
for(int i=0;i<cs.length;i++){
for(int j=0;j<ns.length;j++){
ps[index++]=new Puker(cs[i],ns[j]);
}
}
for(int i=0;i<ps.length;i++){
if(i%ns.length==0){
System.out.println();
}
ps[i].print();
}
}
}
class Puker
{
private String color;
private String number;
Puker(){} // 这里为什么用构造函数?
Puker(String color,String number){ //这个构造函数重载方法,除了给this.属性赋值,还有别的作用吗?那不是跟set方法重了吗???
this.color=color;
this.number=number;
}
void setColor(String color){
this.color=color;
}
String getColor(){
return color;
}
void setNumber(String number){
this.number=number;
}
String getNumber(){
return number;
}
void print(){
System.out.print(color+number+" ");
}
}
|