枚舉 實例


public abstract class City {ide

private City(String address){this

this.address = address;spa

}get

public static final City beijing = new City("北京市"){it

@Overrideclass

public void show() {import

System.out.println("馮威");im

}static

};co

public static final City wuhan = new City("武漢市"){

@Override

public void show() {

System.out.println("焦寧波");

}

};

public static final City zhengzhou = new City("鄭州市"){

@Override

public void show() {

System.out.println("蘇坤");

}

};

private String address;


public String getAddress() {

return address;

}

public abstract void show();

}



public enum City2 {

beijing("北京"){

@Override

public void show() {

System.out.println("馮威哈哈");

}

},wuhan("武漢"){

@Override

public void show() {

System.out.println("焦寧波哈哈");

}

},zhengzhou("鄭州"){

@Override

public void show() {

System.out.println("蘇坤哈哈");

}

};

private City2(String address){

this.address = address;

}

private String address;


public String getAddress() {

return address;

}

public abstract void show();

}



import org.junit.Test;


public class Demo {

@Test

public void fun1(){

//列舉3個分校

/*City beijing = new City();

City wuhan = new City();

City zhengzhou = new City();

City dongjing = new City();*/

System.out.println(City.beijing.getAddress());

System.out.println(City2.beijing.getAddress());

City.beijing.show();

City2.beijing.show();

}

@Test

public void fun2(){

System.out.println( City2.beijing.compareTo(City2.wuhan));

System.out.println( City2.beijing.name());//beijing

City2 beijing = Enum.valueOf(City2.class, "beijing");

beijing.show();

}

}

相關文章
相關標籤/搜索