如何學好java

          如何學好java,其實很簡單,只要用心體會,慢慢積累!
          posts - 106, comments - 7, trackbacks - 0, articles - 3
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          設計模式2_橋橋梁模式

          Posted on 2011-04-22 16:35 哈希 閱讀(121) 評論(0)  編輯  收藏 所屬分類: 設計模式
          好了,開始這是我自己用形狀,大小來描述橋梁模式的方式:
            可以分為四種:大圓,小圓,大矩形,小矩形
            看代碼:
            
          public interface Shape {

              
          public abstract void DrawShape();

          }

          public class Circle implements Shape {

              
          public Circle() {
                  
          // TODO Auto-generated constructor stub
              }

              
          public void DrawShape() {
                  
          // TODO Auto-generated method stub
                  System.out.println("我是圓");
              }
              
          }

          public class Triangle implements Shape {

              
          public Triangle() {
                  
          // TODO Auto-generated constructor stub
              }

              
          public void DrawShape() {
                  
          // TODO Auto-generated method stub
                  System.out.println("我是矩形!");
              }
              
          }

          public abstract class Type {
               
          public abstract void draw(String str);
               
          protected Shape GetShape(String type){
               
          if(type.equals("circle")){
                   
          return new Circle();
               }
          else if(type.equals("triangle")){
                   
          return new Triangle();
               }
          else{
                   
          return Circle();
                  }
               }
              
          private Shape Circle() {
                  
          // TODO Auto-generated method stub
                  return new Circle();
              }
          }

          public class Big extends Type {
              
          private Shape shape;
              
          public Big(String tr){
                  shape
          =GetShape(tr);
              }
              
          public void draw(String str) {
              
          // TODO Auto-generated method stub
              System.out.println(str);
                  System.out.println(
          "");
                  shape.DrawShape();
              }
          }

          public class Small extends Type {
              
          private Shape shape;

              
          public Small(String str) {
                 shape
          =GetShape(str);
              }
              
          public void draw(String tr){
                  System.out.println(tr);
                  System.out.println(
          "");
                  shape.DrawShape();
              }
          }

          可以測試一下啊:
          public class Test {
             
          public static void main(String[] args) {
                 System.out.println(
          "=============小類型================");
                 
          //small類型時候
                 Type type=new Small("triangle");
                 type.draw(
          "triangle");
                 Type type2
          =new Small("circle");
                 type2.draw(
          "circle");
                System.out.println(
          "=============大類型================");
                 
          //Big類型時候
                 Type type3=new Big("circle");
                 type3.draw(
          "circle");
                 Type type4
          =new Big("triangle");
                 type4.draw(
          "triangle");
             }
          }
           設計模式相當強悍啊!能幫助大家學習一點本人心得也算很愉快了!我會繼續吧所學貢獻出來的!


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 云和县| 通江县| 景宁| 垫江县| 沅陵县| 秭归县| 台南市| 义马市| 会理县| 麻江县| 巴塘县| 石屏县| 自治县| 桑日县| 冷水江市| 平武县| 珠海市| 汾阳市| 苗栗市| 旺苍县| 尚志市| 台湾省| 邹城市| 张掖市| 金沙县| 东丰县| 武定县| 延吉市| 楚雄市| 和平区| 扶余县| 铜鼓县| 新化县| 惠水县| 玉树县| 林甸县| 巴东县| 德庆县| 利川市| 蒙阴县| 双鸭山市|