<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    無題

    拿個學位是騙自己的。學問是一輩子的。

    統計

    留言簿(3)

    閱讀排行榜

    評論排行榜

    今天OO考試的一個題

    題目是要求描述算術表達式,操作符僅限于+,-,*,/四種二元操作符,操作數僅限于整數。

    回來想了一下這個題其實是可以用composite模式來做的,UML靜態類圖如下:



    相應的Java代碼如下(沒有考慮除數為零的異常):
    1interface Exp {
    2    float getRes();
    3}

     1public class Two_e_Exp implements Exp {
     2    private Exp A;
     3
     4    private Exp B;
     5
     6    private char op;
     7
     8    /**
     9     * @param a
    10     * @param b
    11     * @param op
    12     */

    13    public Two_e_Exp(Exp a, Exp b, char op) {
    14        A = a;
    15        B = b;
    16        this.op = op;
    17    }

    18
    19    /**
    20     * @return a
    21     */

    22    public Exp getA() {
    23        return A;
    24    }

    25
    26    /**
    27     * @param a
    28     *            要設置的 a
    29     */

    30    public void setA(Exp a) {
    31        A = a;
    32    }

    33
    34    /**
    35     * @return b
    36     */

    37    public Exp getB() {
    38        return B;
    39    }

    40
    41    /**
    42     * @param b
    43     *            要設置的 b
    44     */

    45    public void setB(Exp b) {
    46        B = b;
    47    }

    48
    49    /**
    50     * @return op
    51     */

    52    public char getOp() {
    53        return op;
    54    }

    55
    56    /**
    57     * @param op
    58     *            要設置的 op
    59     */

    60    public void setOp(char op) {
    61        this.op = op;
    62    }

    63
    64    public float getRes() {
    65        // case '+'
    66        float res = A.getRes() + B.getRes();
    67        switch (op) {
    68            case '-'{
    69                res = A.getRes() - B.getRes();
    70                break;
    71            }

    72            case '*'{
    73                res = A.getRes() * B.getRes();
    74                break;
    75            }

    76            case '/'{
    77                res = A.getRes() / B.getRes();
    78                break;
    79            }

    80        }

    81        return res;
    82    }

    83
    84}

     1public class IntNumber implements Exp {
     2    private int number;
     3    
     4    /**
     5     * @param number
     6     */

     7    public IntNumber(int number) {
     8        this.number = number;
     9    }

    10    
    11    /**
    12     * @return number
    13     */

    14    public int getNumber() {
    15        return number;
    16    }

    17
    18    /**
    19     * @param number 要設置的 number
    20     */

    21    public void setNumber(int number) {
    22        this.number = number;
    23    }

    24
    25    public float getRes() {
    26        return number;
    27    }

    28
    29}

    一個簡單的測試程序:
     1public class Main {
     2
     3    /**
     4     * @param args
     5     */

     6    public static void main(String[] args) {
     7        Exp e1 =new Two_e_Exp(new IntNumber(1),new IntNumber(2),'+');
     8        Exp e2 =new Two_e_Exp(new IntNumber(3),new IntNumber(4),'*');
     9        Exp e =new Two_e_Exp(e2,e1,'/');
    10        // (3*4)/(1+2)=4
    11        System.out.println(e.getRes());
    12        ((Two_e_Exp)e1).setA(new IntNumber(2));
    13        // (3*4)/(2+2)=3
    14        System.out.println(e.getRes());
    15        ((Two_e_Exp)e).setA(new Two_e_Exp(new IntNumber(12),new IntNumber(12),'+'));
    16        //(12+12)/(2+2)=6
    17        System.out.println(e.getRes());
    18        //(12+12)/(2+(5-3))=6
    19        ((Two_e_Exp)e1).setB(new Two_e_Exp(new IntNumber(5),new IntNumber(3),'-'));
    20        System.out.println(e.getRes());
    21    }

    22
    23}

    posted on 2007-07-13 23:15 閱讀(1422) 評論(5)  編輯  收藏 所屬分類: My Program

    評論

    # re: 今天OO考試的一個題 2007-07-15 12:29 劉甘泉

    就模式來說,用bridge好的多,題目主要包括兩種抽象,數字實體和操作實體,然后進行組合  回復  更多評論   

    # re: 今天OO考試的一個題 2007-07-16 00:29

    @劉甘泉
    是否可以說詳細點嗎?謝謝!
      回復  更多評論   

    # re: 今天OO考試的一個題 2007-07-16 09:29 GHawk

    可以更進一步Refactoring到Interpreter模式,把switch(op)去掉,給擴展運算符提供支持。  回復  更多評論   

    # re: 今天OO考試的一個題 2007-07-16 10:41 劉甘泉

    把你的代碼修改了一下,把操作分離出來。
    Exp.java
    1/**
    2 * User: liugq
    3 * Date: 2007-7-16
    4 * Time: 10:15:12
    5 */

    6public interface Exp {
    7    float getRes();
    8}

    9
    ExpAbstract.java
     1/**
     2 * User: liugq
     3 * Date: 2007-7-16
     4 * Time: 10:29:26
     5 */

     6public interface ExpAbstract extends Exp {
     7    public  Operation getOp();
     8
     9    public  void setOp(Operation op);
    10
    11    public  Exp getA();
    12
    13    public  void setA(Exp a);
    14
    15    public  Exp getB();
    16
    17    public  void setB(Exp b);
    18}

    19
    IntNumber.java
     1/**
     2 * User: liugq
     3 * Date: 2007-7-16
     4 * Time: 10:18:22
     5 */

     6public class IntNumber implements Exp {
     7    private int number;
     8
     9    public IntNumber(int number) {
    10        this.number = number;
    11    }

    12
    13    public int getNumber() {
    14        return number;
    15    }

    16
    17    public void setNumber(int number) {
    18        this.number = number;
    19    }

    20
    21    public float getRes() {
    22        return number;
    23    }

    24}

    25
    MultiplyOp.java
     1/**
     2 * User: liugq
     3 * Date: 2007-7-16
     4 * Time: 10:20:46
     5 */

     6public class MultiplyOp implements Operation {
     7    private static Operation instance = new MultiplyOp();
     8
     9    public static Operation newInstance() {
    10        return instance;
    11    }

    12
    13    public float execute(Exp a, Exp b) {
    14        return a.getRes() * b.getRes();
    15    }

    16}

    17
    Operation.java
     
    1/**
    2 * User: liugq
    3 * Date: 2007-7-16
    4 * Time: 10:16:33
    5 */

    6public interface Operation {
    7    float execute(Exp a,Exp b);
    8}

    9
    PlusOp.java
     1/**
     2 * User: liugq
     3 * Date: 2007-7-16
     4 * Time: 10:19:37
     5 */

     6public class PlusOp implements Operation {
     7    private static Operation instance = new PlusOp();
     8
     9    public static Operation newInstance() {
    10        return instance;
    11    }

    12
    13    public float execute(Exp a, Exp b) {
    14        return a.getRes() + b.getRes();
    15    }

    16}

    17
    OpFactory.java
     1/**
     2 * User: liugq
     3 * Date: 2007-7-16
     4 * Time: 10:22:18
     5 */

     6public class OpFactory {
     7    public static Operation getPlus() {
     8        return PlusOp.newInstance();
     9    }

    10
    11    public static Operation getMultiply() {
    12        return MultiplyOp.newInstance();
    13    }

    14}

    15
    Two_e_Exp.java
     1/**
     2 * User: liugq
     3 * Date: 2007-7-16
     4 * Time: 10:15:41
     5 */

     6public class Two_e_Exp implements ExpAbstract {
     7    private Exp A;
     8    private Exp B;
     9    private Operation op;
    10
    11    public Two_e_Exp(Exp a, Exp b, Operation op) {
    12        A = a;
    13        B = b;
    14        this.op = op;
    15    }

    16
    17    public Operation getOp() {
    18        return op;
    19    }

    20
    21    public void setOp(Operation op) {
    22        this.op = op;
    23    }

    24
    25    public Exp getA() {
    26        return A;
    27    }

    28
    29    public void setA(Exp a) {
    30        A = a;
    31    }

    32
    33    public Exp getB() {
    34        return B;
    35    }

    36
    37    public void setB(Exp b) {
    38        B = b;
    39    }

    40
    41    public float getRes() {
    42        return op.execute(A, B);
    43    }

    44}

    45
    test.java
     1/**
     2 * User: liugq
     3 * Date: 2007-7-16
     4 * Time: 10:27:02
     5 */

     6public class test {
     7    public static void main(String[] args) {
     8        ExpAbstract e1 = new Two_e_Exp(new IntNumber(1), new IntNumber(2),
     9                OpFactory.getPlus());
    10        ExpAbstract e2 = new Two_e_Exp(new IntNumber(3), new IntNumber(4),
    11                OpFactory.getMultiply());
    12        ExpAbstract e = new Two_e_Exp(e1, e2, OpFactory.getPlus());
    13        System.out.println(e.getRes());
    14    }

    15}

    16
    有點亂   回復  更多評論   

    # re: 今天OO考試的一個題[未登錄] 2007-07-16 21:07

    謝謝樓上的2位指教!  回復  更多評論   

    主站蜘蛛池模板: 午夜免费啪视频在线观看| 日本免费一区二区在线观看| 日韩免费a级毛片无码a∨| 国产日韩成人亚洲丁香婷婷| 亚洲一区二区久久| 男人天堂免费视频| 国产成人3p视频免费观看| 亚洲黄色网址大全| av电影在线免费看| 女人被弄到高潮的免费视频| 五月天网站亚洲小说| 色吊丝性永久免费看码| 国语成本人片免费av无码 | 久久青青成人亚洲精品| 国产精品自拍亚洲| 免费观看的毛片大全 | 亚洲乱理伦片在线观看中字| 未满十八18禁止免费无码网站 | 国内精品免费视频自在线| 亚洲国产精品线在线观看| 人体大胆做受免费视频| 插B内射18免费视频| 精品亚洲aⅴ在线观看| 精品国产福利尤物免费| 免费a在线观看播放| 亚洲午夜精品久久久久久app| 日韩精品无码专区免费播放| 亚洲性猛交XXXX| 一级做a爰黑人又硬又粗免费看51社区国产精品视| 无码人妻一区二区三区免费手机 | 亚洲AV色无码乱码在线观看| 国产精品久久久久久久久免费| 亚洲AV无码欧洲AV无码网站| rh男男车车的车车免费网站| 国产成人免费全部网站| 亚洲中文字幕AV每天更新| 美女视频黄a视频全免费| 亚洲黄色网址在线观看| 国产一精品一AV一免费| 亚洲精品二区国产综合野狼| 国产免费播放一区二区|