静态的方法 不用new对象 可以直接通过类名调用 main() 类BL
非静态方法 不可以直接通过类名调用 getAA()
如果 静态的方法 调用 非静态方法
BL.main()
BL. getAA()* 错
证明:静态方法不可以调用非静态方法
假设:静态方法可以调用非静态方法
类 BL
静态方法main()-----》getAA()
非静态方法getAA()
BL.main() —>getAA()
BL. getAA() 错
斗地主案例
package com.zhongruan;
import java.util.*;
public class Doudizhu {
public static void main(String[] args){
List<String> poker=new ArrayList<>();
String[] nums={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String[] colors={"黑桃","红心","梅花","方块"};
for (String color:colors){
for (String num:nums){
String pai=color+num;
poker.add(pai);
}
}
poker.add("大王");
poker.add("小王");
System.out.println(poker);
Collections.shuffle(poker);
System.out.println(poker);
List<String> dipai=new ArrayList<>();
Random random=new Random();
for (int i=0;i<3;i++){
int index=random.nextInt(poker.size());
String pai=poker.remove(index);
dipai.add(pai);
}
System.out.println(dipai);
System.out.println(poker);
List<String> wwc=new ArrayList<>();
List<String> hll=new ArrayList<>();
List<String> tkh=new ArrayList<>();
for(int i=0;i<poker.size();i++){
String pai=poker.get(i);
int mod=i%3;
if(mod==0){
wwc.add(pai);
}else if(mod==1){
hll.add(pai);
}else {
tkh.add(pai);
}
}
System.out.println(wwc);
System.out.println(hll);
System.out.println(tkh);
class Poke {
private int code;
private String name;
public Poke(int code, String name) {
this.code = code;
this.name = name;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class MyCompare implements Comparator<Poke> {
@Override
public int compare(Poke o1, Poke o2) {
return o1.getCode() - o2.getCode();
}
}
}
}