<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      XuGang

      記錄一個程序員的成長

       

      定義簡單的反射工廠示例


      首先,定義一個水果抽象類,代碼如下:

      class Fruit
      {
          
      //定義虛方法
          public virtual void Eating()
          {
              Console.WriteLine(
      "水果有各種吃法。。。");
          }
      }

       

      然后,實例化幾個水果類,代碼如下:

      class Banana : Fruit
      {
          
      public override void Eating()
          {
              Console.WriteLine(
      "香蕉扒皮吃。。。");
          }
      }

      class Orange : Fruit
      {
          
      public override void Eating()
          {
              Console.WriteLine(
      "橘子剝皮吃。。。");
          }
      }

      class Apple : Fruit
      {
          
      public new void Eating()
          {
              Console.WriteLine(
      "蘋果洗了吃。。。");
          }

          
      //public override void Eating()
          
      //{
          
      //    Console.WriteLine("蘋果洗了吃。。。");
          
      //}
      }

       

      最后,創建水果工廠,代碼如下:

      //水果工廠
      class FruitFactory
      {
          
      //生成水果
          public Fruit CreateFruit(string _fruitname)
          {
              
      //不使用反射的做法如下:
              
      //if ("Apple" == _fruitname)
              
      //{
              
      //    return new Apple();
              
      //}
              
      //else if ("Banana" == _fruitname)
              
      //{
              
      //    return new Banana();
              
      //}
              
      //else if ("Orange" == _fruitname)
              
      //{
              
      //    return new Orange();
              
      //}
              
      //else
              
      //{
              
      //    throw new Exception("您指定的水果不生產!");
              
      //}

              
      //獲得當前程序的命名空間
              string strNamespace = Assembly.GetExecutingAssembly().GetName().Name;

              
      //調用方法一:使用 Type 類
              
      //Type type = Type.GetType("ConsoleApplication1." + _fruitname);
              
      //ConstructorInfo ctorInfo = type.GetConstructor(System.Type.EmptyTypes);
              //// Invoke()方法:返回與構造函數關聯的類的實例。
              //Fruit fruitA = (Fruit)ctorInfo.Invoke(new object[0]);
              
      //return fruitA;

              
      //調用方法二:使用 Assembly 類
              
      //Assembly myAssembly = Assembly.GetExecutingAssembly();
              
      //Fruit fruitB = (Fruit)myAssembly.CreateInstance("ConsoleApplication1." + _fruitname);
              
      //return fruitB;

              
      //調用方法三:使用 Activator 類
              Fruit fruitC = (Fruit)Activator.CreateInstance(Type.GetType("ConsoleApplication1." + _fruitname, falsetrue));
              
      return fruitC;
          }
      }

       

      測試代碼如下:

      class Program
      {
          
      static void Main(string[] args)
          {
              FruitFactory ff 
      = new FruitFactory();

              
      //打?。▉碜愿割惖模核懈鞣N吃法。。。
              Fruit fA = ff.CreateFruit("Apple");
              fA.Eating();

              
      //打印(來自子類的):蘋果洗了吃。。。
              Apple apple = ff.CreateFruit("Apple"as Apple;
              apple.Eating();

              Fruit fB 
      = ff.CreateFruit("Banana");
              fB.Eating();

              Fruit fC 
      = ff.CreateFruit("Orange");
              fC.Eating();
          }
      }

       

       

      利用反射創建實例對象的常用三種方式:

      // 方式一:使用 Type 類
      Type type = Type.GetType("ConsoleApplication1." + _fruitname);
      ConstructorInfo ctorInfo = type.GetConstructor(System.Type.EmptyTypes);
      // Invoke()方法:返回與構造函數關聯的類的實例。
      Fruit fruitA = (Fruit)ctorInfo.Invoke(new object[0]);
      return fruitA;

      // 方式二:使用 Assembly 類
      Assembly myAssembly = Assembly.GetExecutingAssembly();
      Fruit fruitB 
      = (Fruit)myAssembly.CreateInstance("ConsoleApplication1." + _fruitname);
      return fruitB;

      // 方式三:使用 Activator 類
      Fruit fruitC = (Fruit)Activator.CreateInstance(Type.GetType("ConsoleApplication1." + _fruitname, falsetrue));
      return fruitC;


      示例的全部代碼如下:

      View Code
      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Reflection;

      //抽象類可以繼承抽象類

      namespace ConsoleApplication1
      {
          
      class Fruit
          {
              
      //定義虛方法
              public virtual void Eating()
              {
                  Console.WriteLine(
      "水果有各種吃法。。。");
              }
          }

          
      //水果工廠
          class FruitFactory
          {
              
      //生成水果
              public Fruit CreateFruit(string _fruitname)
              {
                  
      //不使用反射的做法如下:
                  
      //if ("Apple" == _fruitname)
                  
      //{
                  
      //    return new Apple();
                  
      //}
                  
      //else if ("Banana" == _fruitname)
                  
      //{
                  
      //    return new Banana();
                  
      //}
                  
      //else if ("Orange" == _fruitname)
                  
      //{
                  
      //    return new Orange();
                  
      //}
                  
      //else
                  
      //{
                  
      //    throw new Exception("您指定的水果不生產!");
                  
      //}

                  
      //獲得當前程序的命名空間
                  string strNamespace = Assembly.GetExecutingAssembly().GetName().Name;

                  
      //調用方法一:使用 Type 類
                  
      //Type type = Type.GetType("ConsoleApplication1." + _fruitname);
                  
      //ConstructorInfo ctorInfo = type.GetConstructor(System.Type.EmptyTypes);
                  //// Invoke()方法:返回與構造函數關聯的類的實例。
                  //Fruit fruitA = (Fruit)ctorInfo.Invoke(new object[0]);
                  
      //return fruitA;

                  
      //調用方法二:使用 Assembly 類
                  
      //Assembly myAssembly = Assembly.GetExecutingAssembly();
                  
      //Fruit fruitB = (Fruit)myAssembly.CreateInstance("ConsoleApplication1." + _fruitname);
                  
      //return fruitB;

                  
      //調用方法三:使用 Activator 類
                  Fruit fruitC = (Fruit)Activator.CreateInstance(Type.GetType("ConsoleApplication1." + _fruitname, falsetrue));
                  
      return fruitC;
              }
          }

          
      class Banana : Fruit
          {
              
      public override void Eating()
              {
                  Console.WriteLine(
      "香蕉扒皮吃。。。");
              }
          }

          
      class Orange : Fruit
          {
              
      public override void Eating()
              {
                  Console.WriteLine(
      "橘子剝皮吃。。。");
              }
          }

          
      class Apple : Fruit
          {
              
      public new void Eating()
              {
                  Console.WriteLine(
      "蘋果洗了吃。。。");
              }

              
      //public override void Eating()
              
      //{
              
      //    Console.WriteLine("蘋果洗了吃。。。");
              
      //}
          }

          
      class Program
          {
              
      static void Main(string[] args)
              {
                  FruitFactory ff 
      = new FruitFactory();

                  
      //打?。▉碜愿割惖模核懈鞣N吃法。。。
                  Fruit fA = ff.CreateFruit("Apple");
                  fA.Eating();

                  
      //打?。▉碜宰宇惖模禾O果洗了吃。。。
                  Apple apple = ff.CreateFruit("Apple"as Apple;
                  apple.Eating();

                  Fruit fB 
      = ff.CreateFruit("Banana");
                  fB.Eating();

                  Fruit fC 
      = ff.CreateFruit("Orange");
                  fC.Eating();
              }
          }
      }

       

      posted on 2011-05-20 13:27  鋼鋼  閱讀(2288)  評論(7)    收藏  舉報

      導航

      主站蜘蛛池模板: 一区二区不卡国产精品| 成人乱人乱一区二区三区| 中文字幕无码乱码人妻系列蜜桃| 亚亚洲视频一区二区三区| 日韩午夜一区二区福利视频| 欧美色欧美亚洲高清在线视频| 成人午夜大片免费看爽爽爽| 人妻体内射精一区二区三区| 国产精品自在线拍国产手青青机版| 国产睡熟迷奷系列网站| 日韩中文字幕v亚洲中文字幕| 国产亚洲精品第一综合另类灬| 最新亚洲人成网站在线影院| 亚洲一区二区三区18禁| 亚洲精品区午夜亚洲精品区| 中文字幕亚洲精品人妻| 亚洲无人区码一二三区别| 麻豆精品久久久久久久99蜜桃| 国产乱码1卡二卡3卡四卡5| 国内偷自第一区二区三区| 中文字幕一区二区三区久久蜜桃| 久久永久视频| 偷拍美女厕所尿尿嘘嘘小便| 性欧美乱熟妇xxxx白浆| 三级国产在线观看| 美女一区二区三区亚洲麻豆| 久久天堂综合亚洲伊人HD妓女| 男女性高爱潮免费网站| 四虎国产精品久久免费精品| 国产精品午夜福利免费看| 国产精品国产亚洲区久久| 国产中文字幕在线精品| 婺源县| 亚洲天堂亚洲天堂亚洲色图| 成年在线观看免费人视频| √天堂中文www官网在线| 亚洲高清WWW色好看美女| 国产精品亚洲电影久久成人影院| 在线一区二区中文字幕| 日本道之久夂综合久久爱| 看亚洲黄色不在线网占|