C#反射
重写与多态
//1. 向上转型 与java 相同,
1 class Vehicle{ 2 public virtual void Run() 3 { 4 Console.WriteLine("VEL RUN"); 5 } 6 7 } 8 class Car : Vehicle{ 9 public override void Run() 10 { 11 Console.WriteLine("Car RUN"); 12 }
}
//重写与隐藏发生的条件
1. member function【method,property....】 2. protected public 3. 函数前面要一致
2.“抽象类”与“开放/关闭”原则 【本质上是不去写重复的代码】
3. 抽象类是为了复用而生,封装确定的内容,将不确定的推迟到合适的子类去实现。
//Relextion
1 //反射 reflex way1 2 //一往都是 new obj 3 // obj.method()... 4 // 反射要求的是动态的追踪obj 5 ITank tank = new HeaveTank(); 6 var t = tank.GetType(); 7 Console.WriteLine("TYPE:" + t); 8 object o = Activator.CreateInstance(t); 9 MethodInfo fireInfo = t.GetMethod("fire"); 10 MethodInfo runmInfo = t.GetMethod("run"); 11 //反向使用函数,更具类型creat一个obj,拿到类型的函数,再用函数调用obj.... 12 fireInfo.Invoke(o, null); 13 runmInfo.Invoke(o, null); 14 15 //反射 reflex way2 16 ServiceCollection sc = new ServiceCollection(); 17 //arg1:实现的service接口类型 arg2:具体实现的class类型 18 //如果service被重复实现,会覆盖之前的实现 19 sc.AddScoped(typeof(ITank), typeof(HeaveTank)); 20 sc.AddScoped(typeof(ITank), typeof(LightTank)); 21 //Driver 需要一个veichle来构造,所有也需要注册Ivechile的interface 22 sc.AddScoped(typeof(IVehicle), typeof(Truck)); 23 sc.AddScoped<Driver>(); 24 ServiceProvider sp = sc.BuildServiceProvider(); 25 //用已经注册好的函数来创建obj 26 ITank tanker = sp.GetService<ITank>(); 27 tanker.fire(); 28 tanker.run(); 29 Driver driver = sp.GetService<Driver>(); 30 driver.Drive();
1 using System; 2 using System.Collections; 3 using System.Reflection; 4 using Microsoft.Extensions.DependencyInjection; 5 6 //一个class一次只做一件事情!---单一直则 7 namespace CSharp_Reflex 8 { 9 public interface IVehicle 10 { 11 void run(); 12 } 13 14 public class Car : IVehicle 15 { 16 public void run() 17 { 18 Console.WriteLine("Car run"); 19 } 20 } 21 22 public class Truck : IVehicle 23 { 24 public void run() 25 { 26 Console.WriteLine("tank run"); 27 } 28 } 29 30 //如果女孩想从car转换到tank的话这个接口就不行 31 // 接口的拆分!! 32 //interface Tank 33 //{ 34 // void fire(); 35 // 运行与开火可以分开 36 // void run(); 37 //} 38 public interface IWeapon 39 { 40 void fire(); 41 } 42 43 interface ITank : IVehicle, IWeapon 44 { 45 } 46 47 //挂载俩插件 48 public class HeaveTank : ITank 49 { 50 public void fire() 51 { 52 Console.WriteLine("BOOM!!!"); 53 } 54 55 public void run() 56 { 57 Console.WriteLine("KAKAKAKA"); 58 } 59 } 60 61 62 63 public class LightTank : ITank 64 { 65 public void fire() 66 { 67 Console.WriteLine("Light BOOM!!!"); 68 } 69 70 public void run() 71 { 72 Console.WriteLine("KAKA"); 73 } 74 } 75 76 public class Driver 77 { 78 private IVehicle _vehicle; 79 public Driver(IVehicle vehicle) 80 { 81 _vehicle = vehicle; 82 } 83 84 public void Drive() 85 { 86 _vehicle.run(); 87 } 88 89 } 90 91 //IEnumerable vs ICollection 92 // collection扩展了enumerable的功能,使其更fat, 93 // foreach 需要实现Ienumerable接口 94 class ReadOnlyCollection : IEnumerable 95 { 96 private int[] _array; 97 public ReadOnlyCollection(int[] array) 98 { 99 _array = array; 100 } 101 102 public IEnumerator GetEnumerator() 103 { 104 return new Enumerator(this); 105 } 106 107 //————————————成员类 108 public class Enumerator : IEnumerator 109 { 110 //composite 一个readonlyCollection 111 private ReadOnlyCollection _collection; 112 private int _head; 113 114 public Enumerator(ReadOnlyCollection collection) 115 { 116 _collection = collection; 117 _head = -1; 118 } 119 120 //读取当前 121 public object Current 122 { 123 get { object o = _collection._array[_head]; return o; } 124 } 125 126 public bool MoveNext() 127 { 128 if (++_head < _collection._array.Length) return true; 129 else return false; 130 } 131 132 public void Reset() 133 { 134 _head = -1; 135 } 136 } 137 } 138 139 //反射机制 140 141 class Program 142 { 143 static void Main(string[] args) 144 { 145 //此时女孩可以自如的切换车🚗 146 Driver girl = new Driver(new HeaveTank()); 147 girl.Drive(); 148 int[] sum1 = new int[] { 1, 2, 3, 4, 5 }; 149 //Collection 接口 150 ReadOnlyCollection roc = new ReadOnlyCollection(sum1); 151 Console.WriteLine(Sum(roc)); 152 153 //反射 reflex way1 154 //一往都是 new obj 155 // obj.method()... 156 // 反射要求的是动态的追踪obj 157 ITank tank = new HeaveTank(); 158 var t = tank.GetType(); 159 Console.WriteLine("TYPE:" + t); // "TYPE: HeaveTank"; 160 object o = Activator.CreateInstance(t); 161 MethodInfo fireInfo = t.GetMethod("fire"); 162 MethodInfo runmInfo = t.GetMethod("run"); 163 //反向使用函数,更具类型creat一个obj,拿到类型的函数,在用函数调用obj.... 164 fireInfo.Invoke(o, null); 165 runmInfo.Invoke(o, null); 166 167 //反射 reflex way2 168 ServiceCollection sc = new ServiceCollection(); 169 //arg1:实现的service接口类型 arg2:具体实现的class类型 170 //如果service被重复实现,会覆盖之前的实现 171 sc.AddScoped(typeof(ITank), typeof(HeaveTank)); 172 sc.AddScoped(typeof(ITank), typeof(LightTank)); 173 //Driver 需要一个veichle来构造,所有也需要注册Ivechile的interface 174 sc.AddScoped(typeof(IVehicle), typeof(Truck)); 175 sc.AddScoped<Driver>(); 176 ServiceProvider sp = sc.BuildServiceProvider(); 177 //用已经注册好的函数来创建obj 178 ITank tanker = sp.GetService<ITank>(); 179 tanker.fire(); 180 tanker.run(); 181 Driver driver = sp.GetService<Driver>(); 182 driver.Drive(); 183 } 184 185 //service 并没有使用额外的collection的方法 186 //如果用Icollection接口则无法使用实现IEnurable接口的class 187 static int Sum(IEnumerable nums) 188 { 189 int sum = 0; 190 //line:101 返回的obj就是n 191 foreach (var n in nums) 192 { 193 sum += (int)n; 194 } 195 return sum; 196 } 197 } 198 }