/*
轉接器設計
 */
package pkg6.pkg41;

//轉接設計

interface Window{
    public void fun1();
    public void fun2();
    public void fun3();
    public void fun4();
    public void fun5();
}

//定義一個抽象類別實現介面的所有方法
//但是,本體為空的

abstract class WindowAdapter implements Window{
    public void fun1(){};
    public void fun2(){};
    public void fun3(){};
    public void fun4(){};
    public void fun5(){};
}

class WindowImp1 extends WindowAdapter{
    public void fun1(){
        System.out.println("執行fun1");
    }
}

class WindowImp2 extends WindowAdapter{
    public void fun2(){
        System.out.println("執行fun2");
    }
}

public class Main {

    
    public static void main(String[] args) {
        Window win = new WindowImp1();  //向上轉型
        win.fun1();
        win.fun2();
        System.out.println("");
        Window win2 = new WindowImp2();  //向上轉型
        win2.fun1();
        win2.fun2();
    }
    
}


執行結果:

執行fun1

執行fun2

文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()


package pkg6.pkg31;

class A {

    public void fun1() {
        System.out.println("A -- > fun1");
    }

    public void fun2() {
        System.out.println("A -- > fun2");
    }
}

class B extends A {

    public void fun1() {
        System.out.println("B -- > fun1");
    }

    public void fun3() {
        System.out.println("B -- > fun3");
    }
}

class C extends A {

    public void fun1() {
        System.out.println("C -- > fun1");
    }

    public void fun5() {
        System.out.println("C -- > fun5");
    }
}

public class Main {

    public static void main(String[] args) {
        A a1 = new C();//向下轉型前,一定要先向上轉型,確認關係
                                //向下轉型 
                                 //為什麼要向下轉型呢?
                                //為了使用 子 類別的個別特殊功能
        fun(a1);

        System.out.println("");

        A a2 = new B();//向下轉型前,一定要先向上車型,確認關係
                                //向下轉型 
                                //為什麼要向下轉型呢?
                                //為了使用 子 類別的個別特殊功能
        fun(a2);
    }
    //此處為向上轉型
    public static void fun(A a) {  //對於乘客來說,他只在意要搭交通工具
        //
        a.fun1();  //不管搭那一個交通工具一定都可以執行的功能

         //根據搭到不同的交通工具,應該可以執行不同的功能
        if (a instanceof B) {
            ((B) a).fun3();      //利用向下轉型,去執行特殊功能 
        }
        if (a instanceof C) {
            ((C) a).fun5();      //利用向下轉型,去執行特殊功能
        }

    }
}


執行結果:

C -- > fun1
C -- > fun5

B -- > fun1
B -- > fun3

文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()


package pkg6.pkg38;

interface Fruit{
    public void eat();
}

class Apple implements Fruit{
    public void eat(){
        System.out.println("**吃Apple");
    }
}
class Orange implements Fruit{
    public void eat(){
        System.out.println("**吃Orange");
    }
}

//工廠設計模式 Factory
class Factory {
    public static Fruit getInstance(String className){
        Fruit f = null;
        if("apple".equals(className)){
            f = new Apple();
        }
        if("orange".equals(className)){
            f = new Orange();
        }
        return f;
    }
}

public class Main {

   
    public static void main(String[] args) {
//        Fruit f = new Apple();
//        f.eat();
//        Fruit f2 = new Orange();
//        f2.eat();
            
            Fruit f =null;
            //透過工廠取得我要的水果
            //我要apple
            f= Factory.getInstance("apple");
            f.eat();
            f= Factory.getInstance("orange");
            f.eat();
           
    }
}


執行結果:

**吃Apple
**吃Orange

文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()

/*

用抽象與介面組成的交通工具 

*/
package hw_03;

import java.util.Scanner;
//交通工具抽象類別
abstract class Transportation {
    public abstract void name();    //名稱
    public abstract void people();      //載客數
    public abstract void speed();       //移動速度
}

//介面A:吃東西
interface A {
    public abstract void eat();
}
//介面B:看電影
interface B {
    public abstract void movie();
}
//介面C:聽音樂
interface C {
    public abstract void music();
}
//介面D:洗手間
interface D {
    public abstract void toilet();
}
//介面E:空姐
interface E {
    public abstract void stewardess();
}

//火車繼承Transportation抽象類別,實現介面A、D
class Train extends Transportation implements A, D {
    int trainPeople = 600;   //火車載客數
    int trainSpeed = 120;       //火車速度
    //覆寫Transportation抽象方法 
    public void name() {
        System.out.println("火車");
    }

    public void people() {
        System.out.println("火車載客數:" + trainPeople + "人");
    }

    public void speed() {
        System.out.println("移動速度:" + trainSpeed + "公里");
    }

    //覆寫介面A、D方法
    public void eat() {
        System.out.println("有賣餐點能吃");
    }

    public void toilet() {
        System.out.println("車廂有洗手間");
    }

    //將要顯示的資料放進show裡
    public void show() {
        //抽象方法 
        name();
        people();
        speed();
        //介面方法
        eat();
        toilet();
    }
}

//計程車繼承Transportation抽象類別 ,實現介面B、C
class Taxi extends Transportation implements B, C {
    //覆寫Transportation抽象方法 
    public void name() {
        System.out.println("計程車");
    }

    public void people() {
        System.out.println("計程車載客人數:4~6人");
    }

    public void speed() {
        System.out.println("移動速度:道路限速");
    }

    //覆寫介面B、C方法
    public void movie() {
        System.out.println("車上電視播放新聞");
    }

    public void music() {
        System.out.println("音響播放警廣交通台");
    }

    //將要顯示的資料放進show裡
    public void show() {
        //抽象方法
        name();
        people();
        speed();
        //介面方法
        movie();
        music();
    }
}

//飛機繼承Transportation抽象類別,實現介面A、D、E
class Aircraft extends Transportation implements A,D,E {
    //覆寫Transportation抽象方法 
    int airPeople = 450;    //載客人數
    int airSpeed = 480;     //移動速度
    public void name() {
        System.out.println("飛機");
    }

    public void people() {
        System.out.println("飛機載客人數:" + airPeople + "人");
    }

    public void speed() {
        System.out.println("移動速度:" + airSpeed + "公里");
    }

    //覆寫介面A、D、E方法
    public void eat() {
        System.out.println("有附餐能吃");
    }

    public void toilet() {
        System.out.println("飛機有洗手間(起飛降落不能使用),限一人使用!");
    }

    public void stewardess() {
        System.out.println("有美麗空服員");
    }

    //將要顯示的資料放進show裡
    public void show() {
        //抽象方法
        name();
        people();
        speed();
        //介面方法
        eat();
        toilet();
        stewardess();
    }
}

public class Hw_03 {

    public static void main(String[] args) {
        //實體化類別物件
        Train train = new Train();
        Taxi taxi = new Taxi();
        Aircraft aircraft = new Aircraft();
        int x = 0;
        Scanner sc = new Scanner(System.in);
        while (x != 2) {
            System.out.println("請輸入要查詢的交通工具種類:1.火車 2.計程車 3.飛機");

            int select = sc.nextInt();
            switch (select) {
                case 1:
                    train.show();
                    break;
                case 2:
                    taxi.show();
                    break;
                case 3:
                    aircraft.show();
                    break;
                default:
                    System.out.println("您輸入的資料不正確!");
                    break;
            }
            System.out.println("1.繼續查詢 2.離開:");
            x = sc.nextInt();
        }
    }
}


執行結果:

請輸入要查詢的交通工具種類:1.火車 2.計程車 3.飛機
1
火車
火車載客數:600人
移動速度:120公里
有火車便當能吃
車廂有洗手間
1.繼續查詢 2.離開:
1
請輸入要查詢的交通工具種類:1.火車 2.計程車 3.飛機
2
計程車
計程車載客人數:4~6人
移動速度:道路限速
車上電視播放新聞
音響播放警廣交通台
1.繼續查詢 2.離開:
1
請輸入要查詢的交通工具種類:1.火車 2.計程車 3.飛機
3
飛機
飛機載客人數:450人
移動速度:480公里
附空中便當
飛機有洗手間(起飛降落不能使用),限一人使用!
有美麗空服員
1.繼續查詢 2.離開:
2

文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()

/*
 用抽象類別建立車子的類別,並建立子類別覆寫抽象類別

產生實體物件並給值實現
 */
package hw_02;
//建立車子的類別

abstract class Car {

    String name;   //名稱
    String color;   //顏色
    int tire;       //輪胎數

    public Car(String name, String color, int tire) {
        this.name=name;
        this.color = color;
        this.tire = tire;
    }

    public String getName() {       //取得名稱資訊
        return name;
    }

    public String getColor() {          //取得顏色資訊
        return color;
    }

    public int getTire() {          //取得輪胎數
        return tire;
    }

    public abstract String show();   //顯示資訊
}

//bus的類別
class Bus extends Car {

    private int people;         //載客數

    public Bus(String name, String color, int tire, int people) {
        super(name, color, tire);     //呼叫CAR類別中的建構方法
        this.people = people;
    }

    public void setPeople() {      //載客數的建構式set
        this.people = people;
    }

    public int getPeople() {        //載客數的建構式get
        return people;
    }

    public String getName() {
        return name;
    }

    public String getColor() {
        return color;
    }

    public int getTire() {
        return tire;
    }

    public String show() {
        return "車名:" + super.getName() + " 顏色:" + super.getColor() + " 輪胎數:" + super.getTire() + " 載客數:" + this.getPeople() + "人";
    }
}

//賓士的類別
class Benz extends Car {

    private int power;         //馬力

    public Benz(String name, String color, int tire, int power) {
        super(name, color, tire);
        this.power = power;
    }

    public void setPower() {
        this.power = power;
    }

    public int getPower() {
        return power;
    }

    public String getName() {
        return name;
    }

    public String getColor() {
        return color;
    }

    public int getTire() {
        return tire;
    }

    public String show() {
        return "車名:" + super.getName() + " 顏色:" + super.getColor() + " 輪胎數:" + super.getTire() + " 馬力:" + this.getPower() + "匹";
    }

}

//腳踏車的類別
class Giant extends Car {

    private int price;         //價錢

    public Giant(String name, String color, int tire, int price) {
        super(name, color, tire);
        this.price = price;
    }

    public void setPrice() {
        this.price = price;
    }

    public int getPrice() {
        return price;
    }

    public String getName() {
        return name;
    }

    public String getColor() {
        return color;
    }

    public int getTire() {
        return tire;
    }

    public String show() {
        return "車名:" + super.getName() + " 顏色:" + super.getColor() + " 輪胎數:" + super.getTire() + " 價錢:" + this.getPrice() + "元";
    }

}

public class Hw_02 {

    public static void main(String[] args) {
        // TODO code application logic here
        Bus bus = new Bus("統聯", "綠色", 12, 40);  //產生實體物件並給值
        System.out.println(bus.show());             //秀出資料

        Benz benz = new Benz("賓士", "紅色", 4, 300);
        System.out.println(benz.show());

        Giant giant = new Giant("捷安特", "藍色", 2, 50000);
        System.out.println(giant.show());

    }

}


執行結果:

車名:統聯 顏色:綠色 輪胎數:12 載客數:40人
車名:賓士 顏色:紅色 輪胎數:4 馬力:300匹
車名:捷安特 顏色:藍色 輪胎數:2 價錢:50000元

文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()

/*
 建立帳號及密碼的陣列,用while迴圈比對輸入的帳號密碼是否正確
驗証碼用亂數產出,如果其中一項錯誤則跳回啟始登入介面
 */
package hw_24;

class Login {//登入系統的類別
    //  帳號的陣列

    public String account[] = {"aaa", "bbb", "ccc"};
    public String password[] = {"111", "222", "333"};

    public int A[] = new int[8];
    public int i;

    // 輸入介面
    public void key_in() {
        String key_id = "";
        String key_pw = "";
        String key_code = "";
        String msg = "";

        boolean flag = true;

        while (flag) {

            System.out.println("請輸入帳號:");
            java.util.Scanner s = new java.util.Scanner(System.in);
            key_id = s.next();

            System.out.println("請輸入密碼:");
            java.util.Scanner s2 = new java.util.Scanner(System.in);
            key_pw = s2.next();

            // 秀出驗證碼
            show_code();
            // 輸入驗證碼
            System.out.println("請輸入驗證碼:");
            java.util.Scanner s3 = new java.util.Scanner(System.in);
            key_code = s3.next();

            // 呼叫 check(    )
            switch (check(key_id, key_pw, key_code)) {
                case 1:
                    msg = "登入成功";
                    break;
                case 2:
                    msg = "帳號錯誤";
                    break;
                case 3:
                    msg = "密碼錯誤";
                    break;
                case 4:
                    msg = "驗證碼錯誤";
                    break;
                default:
                    msg = "系統異常";
                    break;
            }
            System.out.println(msg);
        }
    }

    public void show_code() {
        System.out.print("驗證碼為:");
        //      驗證碼
        for (i = 0; i < 8; i++) {
            if (i < 3) {                                           //前 3 放數字
                A[i] = (int) ((Math.random() * 10) + 48);
            } else if (i < 6) {                                           // 中間 3 位放大寫英文
                A[i] = (int) (((Math.random() * 26) + 65));
            } else {                                           // 後 2 位放小寫英文
                A[i] = ((int) ((Math.random() * 26) + 97));
            }

            System.out.print((char) A[i]);
        }

        System.out.println();
    }

    // 驗證登入資料
    public int check(String id, String pw, String code) {
        int op = 0;
        //  檢查如果正確傳回 1
        //  帳號錯傳回 2
        //  密碼錯傳回 3
        //  驗證碼錯傳回4
        for (i = 0; i < account.length; i++) {
            String code_t = "";
            for (int j = 0; j < 8; j++) {
                code_t += (char) A[j];
            }
            if (id.equals(account[i]) && pw.equals(password[i]) && code.equals(code_t)) {
                op = 1;
                return op;
            } else if (id.equals(account[i])) {
                if (pw.equals(password[i])) {
                    op = 4;
                    return op;
                } else {
                    op = 3;
                    return op;
                }
            } else {
                op = 2;
                return op;
            }
        }
        return op;
    }
}

public class Hw_24 {

    public static void main(String[] args) {
        // 建立一個系統登入的物件
        Login s_login = new Login();
        // 開啟系統
        s_login.key_in();
    }

}

文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()

/*
 先建立題庫,再建立空的陣列來輸入答案
接著答案比對題庫
再統計出答題跟答錯的題目數
 */
package hw_23;

class EnglishType {

    //  題庫
    public String exam_1[] = {"apple", "box", "cake", "deal", "effect"};
    public String keyin_value = "";    //宣告輸入答案的字串
    public String ans_1[] = new String[exam_1.length];   //練習答案輸入的陣列

    public int i;

    //  啟動的地方
    public void play() {
        for (i = 0; i < exam_1.length; i++) {
            showExam();     //秀出題目 
            keyin();            //輸入答案
            //check_str( keyin_value);
            ans_1[i] = keyin_value;
        }

        count_data();
    }

    // 秀題目
    public void showExam() {
        System.out.println((i + 1) + ".題目是:" + exam_1[i]);
    }

    //  輸入介面
    public void keyin() {
        System.out.println("請輸入答案:");
        java.util.Scanner s = new java.util.Scanner(System.in);
        keyin_value = s.next();
    }

    //  統計結果
    public void count_data() {

        int t = 0;
        int f = 0;
        System.out.println("*****開始比對答案*****");
        try {
            Thread.sleep(2000);         //  毫秒
        } catch (Exception e) {
        }
        for (i = 0; i < exam_1.length; i++) {   
            System.out.println((i + 1) + ".題目是:" + exam_1[i]);

            System.out.println("您輸入的答案為:" + ans_1[i]);

            if (ans_1[i].equals(exam_1[i])) {       //比對與題目陣列的答案
                t += 1;             //正確+1
            } else {
                f += 1;             //錯誤+1
            }
        }

        System.out.println("總共答對:" + t + "\t答錯:" + f);
    }

}

public class Hw_23 {

    public static void main(String[] args) {
        // 建立一個可以英打練習的物件
        EnglishType type = new EnglishType();

        //開始練打
        type.play();
    }

}

文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()

 

package hw_26;

import java.util.*;
import java.text.SimpleDateFormat;

class ATM {                 //提款機類別

    String password = "1234";//設定卡片密碼
    static int accountMoney = 50000;    //帳號內有5萬塊

    String key_pw = " ";  //輸入密碼
    int getMoney = 0; //提款金額
    int saveMoney = 0; //存款金額
    int transMoney = 0; //轉帳金額
    String trans_id = " ";   //轉帳代碼
    String trans_name = " ";  //轉帳帳號

    java.util.Scanner s = new java.util.Scanner(System.in);

    public void Key_in() {      //密碼輸入介面
        int count = 0;   //錯誤計數器
        do {
            System.out.print("請輸入密碼:");
            key_pw = s.next();
            if (key_pw.equals(password)) {          //比對密碼是否相同
                System.out.println("登入成功");
                Show();
            } else {
                System.out.print("密碼錯誤");
                count++;                        //密碼比對錯誤count累加一次
            }
        } while (count < 3);
        if (count == 3) {    //錯3次跳出輸入介面
            System.out.println("密碼錯誤3次,請洽櫃檯人員");
        }
    }

    //顯示操作頁面
    public void Show() {
        System.out.println("請選擇要使用的功能:");
        System.out.println("1.提款  2.存款  3.轉帳  4.餘額查詢 5.離開");
        switch (s.nextInt()) {
            case 1:
                this.getMoney();  //呼叫提款功能
                break;
            case 2:
                this.saveMoney();  //呼叫存款功能
                break;
            case 3:
                this.transMoney();//呼叫轉帳功能
                break;
            case 4:                     //餘額查詢
                System.out.println("帳戶還有" + accountMoney + "元。");
                final_Money();
                break;
            default:
                System.out.println("謝謝光臨,歡迎再度使用" + "\n");
                break;
        }
    }

    //提款功能
    public void getMoney() {
        System.out.println("請輸入提領金額");
        this.getMoney = s.nextInt();
        System.out.println("您欲提領" + this.getMoney + "元。正確請按1,錯誤請按2");
        switch (s.nextInt()) {
            case 1:
                if (accountMoney < getMoney) {
                    System.out.println("餘額不足,無法提領");
                } else {
                    accountMoney -= getMoney;
                }
                break;
            default:
                System.out.println("取消提款...");
        }
        getListCheck();
        use_again();
    }

    //存款功能
    public void saveMoney() {
        System.out.print("請輸入存入金額:");
        this.saveMoney = s.nextInt();
        System.out.println("辨鈔中");
        try {                 //延遲時間設定
                    Thread.sleep(3000);//延遲3秒
                } catch (InterruptedException ex) {
                }
        System.out.println("您欲存入金額為:" + this.saveMoney + "元。正確請按1,錯誤請按2");
        
        if (s.nextInt() == 1) {
            accountMoney += this.saveMoney;
            saveListCheck();
            use_again();
        } else {
            System.out.print("取消操作");
        }
    }

    //轉帳功能
    public void transMoney() {
        System.out.println("請輸入轉帳代碼(3位數)");
        trans_id = s.next();
        System.out.println("請輸入轉帳帳號");
        trans_name = s.next();
        System.out.println("請輸入轉帳金額(每筆金額不可超過3萬元)");
        this.transMoney = s.nextInt();
        System.out.println("轉帳代碼" + trans_id);
        System.out.println("轉帳帳號" + trans_name);
        System.out.println("轉帳金額" + this.transMoney);
        System.out.println("以上資料是否正確?確定請按1,重新操作請按2");
        int check;
        check = s.nextInt();
        if (check == 1) {
            if (accountMoney < transMoney) {
                System.out.println("餘額不足,轉帳失敗");
            } else if (transMoney > 30000) {
                System.out.println("金額不可超過3萬元,請重新操作");
            } else {
                this.accountMoney = (accountMoney - transMoney) - 6;
                transListCheck();
                use_again();
            }
        } else {
            System.out.println("重新操作");
        }
    }

    //提款交易明細表 
    public void getListCheck() {
        System.out.println("是否列印明細表? 1.列印 2.不列印 3.螢幕顯示");
        int choice = s.nextInt();
        switch (choice) {
            case 1:
                System.out.println("明細表列印中,請稍候...");
                try {                 //延遲時間設定
                    Thread.sleep(3000);//延遲3秒
                } catch (InterruptedException ex) {
                }
                System.out.println("________________________________________");
                System.out.println("|\t交易\t明細表\t\t\t|");
                System.out.println("|時間:\t\t" + getTime() + "\t|");
                System.out.println("|提領\t金額:\t" + getMoney + "\t\t元\t|");
                System.out.println("|餘額:\t\t" + accountMoney + "\t\t元\t|");
                System.out.println("________________________________________");
                break;
            case 2:
                break;
            case 3:
                System.out.println("時間:" + getTime());
                System.out.println("提領金額:" + getMoney + "元");
                System.out.println("帳戶餘額:" + accountMoney + "元");
                break;
            default:
                break;
        }
        use_again();
    }

    //存款交易明細表 
    public void saveListCheck() {
        System.out.println("是否列印明細表? 1.列印 2.不列印 3.螢幕顯示");
        int choice = s.nextInt();
        switch (choice) {
            case 1:
                System.out.println("明細表列印中,請稍候...");
                try {                 //延遲時間設定
                    Thread.sleep(3000);//延遲3秒
                } catch (InterruptedException ex) {
                }
                System.out.println("________________________________________");
                System.out.println("|\t交易\t明細表\t\t\t|");
                System.out.println("|時間:\t\t" + getTime() + "\t|");
                System.out.println("|存款\t金額:\t" + saveMoney + "\t\t元\t|");
                System.out.println("|餘額:\t\t" + accountMoney + "\t\t元\t|");
                System.out.println("________________________________________");
                break;
            case 2:
                break;
            case 3:
                System.out.println("時間:" + getTime());
                System.out.println("存款金額:" + saveMoney + "元");
                System.out.println("帳戶餘額:" + accountMoney + "元");
                break;
            default:
                break;
        }
        use_again();
    }

    //轉帳交易明細
    public void transListCheck() {
        System.out.println("明細表列印中,請稍候...");
        try {                 //延遲時間設定
            Thread.sleep(3000);//延遲3秒
        } catch (InterruptedException ex) {
        }
        System.out.println("________________________________________");
        System.out.println("|\t交易\t明細表\t\t\t|");
        System.out.println("|時間:\t\t" + getTime() + "\t|");
        System.out.println("|轉帳代碼:" + trans_id);
        System.out.println("|轉帳帳號:" + trans_name);
        System.out.println("|轉帳\t金額:\t" + transMoney + "\t\t元\t|");
        System.out.println("|餘額:\t\t" + accountMoney + "\t\t元\t|");
        System.out.println("________________________________________");
        use_again();
    }

    //餘額查詢明細表 
    public void final_Money() {
        System.out.println("是否列印明細表? 1.列印 2.不列印 3.螢幕顯示");
        int choice = s.nextInt();
        switch (choice) {
            case 1:
                System.out.println("明細表列印中,請稍候...");
                try {                 //延遲時間設定
                    Thread.sleep(3000);//延遲3秒
                } catch (InterruptedException ex) {
                }
                System.out.println("________________________________________");
                System.out.println("|\t交易\t明細表\t\t\t|");
                System.out.println("|時間:\t\t" + getTime() + "\t|");
                System.out.println("|餘額:\t\t" + accountMoney + "\t\t元\t|");
                System.out.println("________________________________________");
                break;
            case 2:
                break;
            case 3:
                System.out.println("時間:" + getTime());
                System.out.println("存款金額:" + saveMoney + "元");
                System.out.println("帳戶餘額:" + accountMoney + "元");
                break;
            default:
                break;
        }
        use_again();
    }

    //取得現在時間
    public String getTime() {
        SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        Date date = new Date();
        String strDate = sdFormat.format(date);
        return strDate;
    }

    //是否繼續使用
    public void use_again() {
        System.out.println("請選擇是否繼續使用 1:是(請插入金融卡) 2:否");
        Scanner c = new Scanner(System.in);
        int choice = c.nextInt();
        if (choice == 1) {
            Key_in();
        } else {
            System.out.println("晶片經融卡已退出,請取回");
        }
    }

}

public class Hw_26 {

    public static void main(String[] args) {
        // TODO code application logic here
        ATM a = new ATM();
        a.Key_in();
    }
}

文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()

package hw_poker;

import java.util.Random;

class Card {
    int Suit;                                   //花色
    char Number;                            //牌面數字 
}
public class Hw_poker {
    static Card[] OneCard = new Card[52];  //儲存每張撲克的花色、數字
    //顯示撲克牌
    static void showCard() {
        int i, j;
        String s = "";     //代表花色

        for (i = 0, j = 0; i < 52; i++, j++) //撲克牌迴圈一遍
        {
            if (j % 13 == 0) {                      //控制每種花色一行
                System.out.print("\n");
            }
            switch (OneCard[i].Suit) {          //枓據花色屬性,顯示花色符號
                case 1:                             //1,代表黑桃
                    s = "黑桃";
                    break;
                case 2:                             //2,代表紅心
                    s = "紅心";
                    break;
                case 3:                              //3,代表方塊
                    s = "方塊";
                    break;
                case 4:                              //4,代表梅花
                    s = "梅花";
                    break;
                default:
                        ;
            }
            System.out.printf("    " + s + OneCard[i].Number);   //輸出顯示
        }
        System.out.print("\n");
    }

    //洗牌
    static void shuffle() {
        int i, j;
        Card tempcard = new Card();         //交換過程中臨時備份用
        Random r = new Random();                //隨機種子
        for (i = 0; i < 52; i++) //換52次牌
        {
            j = r.nextInt(52);                      //隨機取牌
            tempcard = OneCard[j];          //換牌
            OneCard[j] = OneCard[i];
            OneCard[i] = tempcard;
        }
    }

    public static void main(String[] args) {
        initial();       //產生一副新牌
        System.out.printf("一副新牌的初始排列如下:\n");
        showCard();                   //顯示新牌的排列
        shuffle();                      //洗牌
        System.out.print("\n洗牌後的排列如下:\n");
        showCard();                 //顯示洗牌後的排列
    }
    //產生一副新牌
    private static void initial() {
        int i, temp;
        int suit;
        suit = 0;
        for (i = 0; i < 52; i++) //產生52張牌
        {
            if (i % 13 == 0) //每13張牌改變花色
            {
                suit++;                         //改變花色
            }
            Card t = new Card();        //產生撲克牌物件
            t.Suit = suit;                   //儲存花色
            temp = i % 13;              //牌面數字不超過13
            switch (temp) {
                case 0:
                    t.Number = 'A';         //第一個數從0開始,代表 A
                    break;
                case 9:
                    t.Number = '0';         //代表A
                    break;
                case 10:
                    t.Number = 'J';         //代表J
                    break;
                case 11:
                    t.Number = 'Q';         //代表Q
                    break;
                case 12:
                    t.Number = 'K';         //代表K
                    break;
                default:
                    t.Number = (char) (temp + '1');    //其他普通數字,注意加1
            }
            OneCard[i] = t;                     //儲存撲克
        }
    }
}


執行結果:

一副新牌的初始排列如下:

    黑桃A    黑桃2    黑桃3    黑桃4    黑桃5    黑桃6    黑桃7    黑桃8    黑桃9    黑桃0    黑桃J    黑桃Q    黑桃K
    紅心A    紅心2    紅心3    紅心4    紅心5    紅心6    紅心7    紅心8    紅心9    紅心0    紅心J    紅心Q    紅心K
    方塊A    方塊2    方塊3    方塊4    方塊5    方塊6    方塊7    方塊8    方塊9    方塊0    方塊J    方塊Q    方塊K
    梅花A    梅花2    梅花3    梅花4    梅花5    梅花6    梅花7    梅花8    梅花9    梅花0    梅花J    梅花Q    梅花K

洗牌後的排列如下:

    黑桃4    紅心7    紅心0    黑桃5    方塊0    紅心3    黑桃9    方塊9    方塊3    黑桃2    黑桃0    黑桃J    紅心J
    方塊J    紅心6    方塊4    紅心K    黑桃A    梅花7    梅花A    梅花Q    紅心2    梅花K    紅心8    黑桃7    方塊2
    紅心9    黑桃3    梅花4    梅花J    黑桃6    紅心A    梅花5    方塊7    梅花6    梅花8    方塊A    方塊6    紅心Q
    紅心4    黑桃K    黑桃Q    方塊5    紅心5    黑桃8    梅花9    方塊K    方塊8    梅花0    梅花2    方塊Q    梅花3

文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()

目標:

  • 掌握匿名內部類別的作用
  • 掌握匿名內部類別的定義格式

匿名內部類別是指沒有一個實際名稱的類別,此概念是在介面和抽象類別應用上發展起來的

內部類別的操作

interface A{
    public void printInfo() ;    // 定義抽象方法 
}
class B implements A{                                       //定義介面實現類別 
    public void printInfo(){                    //覆寫抽象方法 
        System.out.println("Hello World!!!") ;
    }
};
class X {                                                           //定義X類別
    public void fun1(){                         //定義fun()方法
        this.fun2(new B()) ;       //傳遞子類別實例
    }
    public void fun2(A a){                      //接收介面實例
        a.printInfo() ;                 //呼叫介面方法 
    }
};
public class NoInnerClassDemo{
    public static void main(String args[]){
        new X().fun1() ;            //產生實體X類別物件並呼叫fun()方法 
    }
};

執行結果:

Hello World!!!


匿名內部類別

interface A{                                     // 定義介面A
    public void printInfo() ;    // 定義抽象方法 
}
class X {                                           //定義X類別
    public void fun1(){             //定義fun()方法 
        this.fun2(new A(){              //匿名內部類別
                public void printInfo(){        //實現介面中的抽象方法 
                    System.out.println("Hello World!!!") ;
                }
            }
            ) ;
    }
    public void fun2(A a){                  //接收介面實例
        a.printInfo() ;             //呼叫介面方法 
    }
};
public class NoInnerClassDemo{
    public static void main(String args[]){
        new X().fun1() ;        // 產生實體X類別物件並呼叫fun1()方法
    }
};

執行結果:

Hello World!!!


總結:

  1. 在實際的Java開發中經常會使用到各種開發的框架,在框架上會大量的引用匿名內部類。
  2. 匿名內部類是在抽象類別和接面的基礎之上發展起來的。
文章標籤

mingyilai 發表在 痞客邦 留言(0) 人氣()