在网上看到一个设计扑克牌的代码   发现其中有一段代码是Cards c:PkC.getNewCards();
Cards是一个类,PkC也是一个类,那中间的:是什么意思?

解决方案 »

  1.   

    这是一句完整的代码吗?没见过这样的;如果是这样还能理解for(Cards c:PkC.getNewCards();){
    ...
    }这个是JDK1.5的增强For循环语句
    这个的意思是遍历PKC.getNewCards();将其中的值挨个赋给Cards c;
    PKC.getNewCards()必须是一个List或Set或是数组
      

  2.   

    三楼说的好像是for-each循环,但是for-each循环的话
    for(Cards c:PkC.getNewCards();)
    这一句是不是不用那个分号了,还是这个不是for-each循环了,??????
      

  3.   

    class CardsType {
        private static final String [] ct={"黑桃","红桃","梅花","方块"};
        public static String getType(int i)
        {
            return ct[i];
        }
        public static int com(String s,String c)
        {
            int s1=find(s);
            int s2=find(c);
            return s2-s1;
        }
        private static int find(String s)
        {
            int i=0;
            for(String st:ct)
            {
                i++;
                if(st.equals(s))
                    return i;
            }
            return i;
        }
    }class Num {
        private static final String n[]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};    public static String getN(int i)
        {
            if(i<0||i>12)
                return null;
            return n[i];
        }
    }class Cards implements Comparable{
        private String type;
        private int num;    public Cards(){}
        public Cards(String s,int n)
        {
            this.type=s;
            this.num=n;
        }
        public String getType()
        {
            return type;
        }
        public void setType(String type)
        {
            this.type = type;
        }
        public int getNum()
        {
            return num;
        }
        public void setNum(int num)
        {
            this.num = num;
        }    public boolean equals(Object o)
        {
            Cards c=(Cards)o;
            if(this.num==c.getNum()&&this.type.equals(c.getType()))
                return true;
            return false;
        }    public int compareTo(Object o)
        {
            Cards c=(Cards)o;
            int bjjg;
            if((bjjg=CardsType.com(this.type,c.getType()))!=0)
                return bjjg;
            return c.getNum()-this.num;
        }    public String toString()
        {
            return type+" : "+Num.getN(num);
        }
    }
    这是一个扑克牌游戏的代码的一部分……刚学java……不太明白……那些实现Comparable接口的函数是什么意思?……
      

  4.   

    应该是foreach语句 是新特性啊