<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位指教!  回復  更多評論   

    主站蜘蛛池模板: 水蜜桃视频在线观看免费| 国产亚洲精品免费| 久久99久久成人免费播放| 亚洲日本VA午夜在线电影| 久久久久亚洲av无码专区| 美腿丝袜亚洲综合| 免费**毛片在线播放直播| 无码少妇一区二区浪潮免费| 嫩草影院在线播放www免费观看| 免费夜色污私人影院网站电影| 狠狠色香婷婷久久亚洲精品| 亚洲高清无在码在线电影不卡| 亚洲精品国产精品乱码不卡√| 亚洲国产精品尤物yw在线| 国产片免费在线观看| 成人免费男女视频网站慢动作| 91免费国产在线观看| 一级毛片在线免费观看| 国精产品一区一区三区免费视频 | 久久99青青精品免费观看| 中美日韩在线网免费毛片视频| 亚洲精品无码专区在线播放| 亚洲午夜在线播放| 亚洲av无码不卡久久| 亚洲日本国产精华液| 久久亚洲AV成人出白浆无码国产| 亚洲va无码va在线va天堂| 亚洲色成人WWW永久网站| 国产亚洲精品资在线| 久久影视综合亚洲| 亚洲热线99精品视频| 国产亚洲精品a在线无码| 亚洲精品tv久久久久久久久| 亚洲AV综合色区无码另类小说| 亚洲精品无码高潮喷水在线| 中文字幕亚洲天堂| 国产成A人亚洲精V品无码| 久久精品亚洲日本佐佐木明希| 亚洲一级二级三级不卡| 亚洲精品第一国产综合精品| 亚洲日韩在线视频|