构建一个类名为Poker
属性包含花色,点数String color;String dot;
用数组保存牌
int num[]=new int[30];//Array类型
num.length为数组长度
无法删除数据
常用定义方式:String[] titles=new String[]{"a","b"};
增强for循环
for(String temp : titles){
System.out.println(temp);
}
生成一副牌,准备一个数组保存所有点数
{
String[] dots=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String[] colors=new String[]{"♥","♠","♦","♣"};
//数组保存所有牌
Poker[] pokers=new Poker[52];
int index=0;
for(String dot : dots){
for(String color : colors){
//创建一张牌
Poker poker=new Poker();
poker.dot=dot;
poker.color=color;
pokers[index]=poker;
index++;
}
}
//输出一副牌
for(Poker poker : pokers){
System.out.print(poker.dot+poker.color+" ");
}
类->内部类
方法
java里面方法是不能独立存在的,只能在类中声明
类方法=静态方法 (用static修饰) 不用对象就能使用
对象方法=实例方法 创建一个对象后才能使用
静态方法比实例方法优先被加载
当这个类被加载到内存时,静态的东西先被分配内存,创建一个类的对象,才分配其他内存,所以静态方法不能调用其他东西,对象不能调用静态方法
对象方法依附于对象,用对象调用对象方法
static可修饰变量,方法,内部类
Java参数传递机制:值传递(传递的是拷贝(形参))
用对象当作参数,相当于传递地址
可变参数...数组
public void test(String...args){
}
test(...args:“a”,“b”);
这里的args为数组
通过使用数组接收,等价于String[] args
方法的重载:同一个类里面方法名相同,参数不同
方法的重载和返回值修饰符没有关系,只有参数不同才能重载
构造方法,系统提供的无参数方法,用来创建对象
可以自己写构造方法,可以提前赋值
特点:方法名和类名相同,没有返回值
可以使代码更简洁,可以保证使用时有数据