<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片2008| 亚洲人成77777在线播放网站不卡 亚洲人成77777在线观看网 | 亚洲heyzo专区无码综合| 一级做a爰片久久毛片免费陪| 巨胸喷奶水视频www免费视频| 无码国产亚洲日韩国精品视频一区二区三区 | 亚洲另类无码一区二区三区| 成人免费无码大片A毛片抽搐 | 日韩高清免费观看| 亚洲一区二区女搞男| 亚洲成A人片在线播放器| 毛片免费视频播放| 亚洲aⅴ无码专区在线观看春色| 精品免费国产一区二区三区 | 久久成人免费大片| 亚洲精品不卡视频| 国产啪精品视频网免费| 亚洲av中文无码字幕色不卡 | 亚洲最新视频在线观看| 成年网站免费视频A在线双飞| 亚洲人成人伊人成综合网无码| 国产一精品一aⅴ一免费| youjizz亚洲| 四虎成人精品在永久免费| 美女网站在线观看视频免费的 | 国产男女性潮高清免费网站| 丰满少妇作爱视频免费观看| 四虎在线视频免费观看| ww亚洲ww在线观看国产| 91人人区免费区人人| 亚洲欧美中文日韩视频| 伊人亚洲综合青草青草久热| 中文字幕免费在线观看| 久久久久亚洲国产AV麻豆| 亚洲老妈激情一区二区三区| 无码国产精品久久一区免费| 一级**爱片免费视频| 亚洲六月丁香六月婷婷色伊人| 免费在线观看黄网| 亚洲免费闲人蜜桃| 亚洲中文无码av永久|