1.
// 範例:EX7_30.java
public class EX7_30
{
enum YourColor {PINK, IVORY, ORANGE, WHITE, GOLDEN};
enum MyColor {RED, GREEN, BLUE, YELLOW}
//程式起始方法
public static void main(String [] args)
{
MyColor mc = MyColor.GREEN;
MyColor mc2 = MyColor.GREEN;
YourColor yc = YourColor.IVORY;
YourColor yc2 = EX7_30.YourColor.WHITE;
echo( yc2 );
echo("yc2.equals(\"WHITE\"): "+ yc2.equals("WHITE") );
//echo("yc > yc2: "+ (yc>yc2) ); //錯誤
//echo("yc==1: "+ (yc==1) ); //錯誤,不同型別
//echo("yc==mc: "+ (yc==mc) ); //錯誤,不同型別
echo("yc==yc2: "+ (yc==yc2) );
echo("mc==mc2: "+ (mc==mc2) );
echo("yc.compareTo(yc2): "+ yc.compareTo(yc2) );
echo("mc.compareTo(mc2): "+ mc.compareTo(mc2) );
echo("mc.valueOf(mc2): "+ mc.valueOf("YELLOW") );
}
//自訂方法
static void echo(Object str)
{
System.out.println(str);
}
}
2.
class QA7_23
{
enum rank{甲,乙,丙,丁};
public static void main(String [] args)
{
rank 甲 = rank.甲;
rank 乙 = rank.乙;
System.out.println(甲);//甲
System.out.println(甲 != rank.甲);//F
System.out.println(甲 == rank.甲);//T
System.out.println(甲.toString());//甲
System.out.println(rank.valueOf("乙"));//乙
System.out.println(甲.compareTo(乙));//乙
for(rank a: rank.values()) System.out.print(a);
}
}
3.(需配合JC.BAT)
class QA7_24
{
enum rank{甲,乙,丙,丁};
public static void main(String [] args)
{
rank a;
if (args.length>=1){
a=rank.valueOf(args[0]);
}else{
return;
}
switch(a){
case 甲:
System.out.println("甲");
break;
case 乙:
System.out.println("乙");
break;
case 丙:
System.out.println("丙");
break;
case 丁:
System.out.println("丁");
break;
}
}
}
4.
class EX8_1 {
public static void main(String [] args) {
EX8_1 e1 = new EX8_1();
EX8_1 e2 = new EX8_1();
EX8_1 e3 = e1; //指向相同的物件實體
System.out.println("e1和e2一樣: " + e1.equals(e2));
System.out.println("e1和e3一樣: " + e1.equals(e3));
System.out.println(e1==e3);
}
}
5.
class EX8_2 {
public static void main(String [] args) {
學生 a = new 學生();
System.out.println(a.姓名); System.out.println(a.生日);
System.out.println(a.學號); a.showAtt(); //呼叫物件方法
}
}
class 個人 {
String 姓名 = "李小明";
String 生日 = "1990-01-01";
void showAtt() { System.out.println("姓名:"+姓名+" 生日:"+生日); }
}
class 學生 extends 個人 { int 學號 = 9012345; }
6.QA8_1
class A{ int i = 10; }
class QA8_1 extends A{
static int i=100;
public static void main(String[] args){
QA8_1 t=new QA8_1();
System.out.println(t.i);
}
}
7.QA8_2
class Super{
static int i=10;
}
final class QA8_1 extends Super{
static int i=5;
public static void main (String[] args){
System.out.println(i);
}
}
8.
留言列表