1 using System; //导入命名空间system中的类 2 using System.Collections.Generic; //导入System.Collections.Generic中的类 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 //using 导入命名空间 7 //使用微软提供的命名空间,使用里面的类 8 9 10 11 12 13 14 15 16 17 namespace CShapeDemo //命名空间CShapeDemo 包含了Program 等若干个类 18 { 19 20 21 //接口类 22 interface ICatchMice 23 { 24 void CatchMice();//默认是public ,且不能被private protected限定 25 } 26 27 interface IClimbTree 28 { 29 void ClimbTree(); 30 } 31 32 33 abstract public class Pet //父类,当包含abstract时,类也要定义成abstract类 34 { 35 //构造函数 36 public Pet() 37 { 38 } 39 40 public Pet(string tempName) 41 { 42 this.name = tempName; 43 } 44 45 46 //行为 47 public void printname() 48 { 49 Console.WriteLine("Pet's name is " + name); 50 } 51 52 //重载 53 virtual public void Speek() //父类的虚函数 54 { 55 Console.WriteLine("Pet is Speaking !"); 56 } 57 58 abstract public void Speek2();//抽象方法 59 60 61 //非静态数据 62 protected string name; 63 64 } 65 66 67 public class Dog : Pet,ICatchMice,IClimbTree //子类只能继承1个基类,但是可以继承多个接口类 68 { 69 //构造函数 70 public Dog(string name) : base(name) // base(name) 是基类的构造函数... 根据参数的类型和个数可以选择不同的父类构造函数 71 { 72 Num++;//静态计数器 73 } 74 75 new public void printname() //new 表示子类的这个函数和父类的函数重名,是个签名函数...有没有new都可以实现隐藏...最好是加上new 76 { 77 Console.WriteLine("宠物的 name is " + name); 78 } 79 //静态构造函数 :只负责初始化静态成员的数据 80 static Dog() //静态构造函数不能加public 81 { 82 Num = 0; 83 } 84 //静态函数 85 static public void ShowAllDogNum() 86 { 87 Console.WriteLine("一共创建了{0}个狗", Num); 88 } 89 90 91 92 //重写 //多态会重写,实现子类的差异化 93 public override void Speek() // public override 和 override public 是同一个意思 就像 const int 一样 94 { 95 // base.Speek(); 96 Console.WriteLine("狗叫"); 97 } 98 99 //抽象方法 100 sealed public override void Speek2() //抽象方法在子类必须用override关键字实现,实现差异化 101 { 102 Console.WriteLine("狗子在叫呢"); //sealed关键字表示该方法被继承的时候,也不允许Dog的子类重写它 103 } 104 105 106 //接口类的方法必须要实现,且 用public修饰 107 public void CatchMice() 108 { 109 Console.WriteLine("老子是狗,不会捉老鼠的"); 110 } 111 public void ClimbTree() 112 { 113 Console.WriteLine("老子是狗,也不会爬树呀"); 114 } 115 116 117 118 //静态数据 119 static int Num; 120 121 122 //自定义转换 123 public static implicit operator Cat(Dog dog) //public static implicit operator 特定写法,隐式转换 124 { 125 //把狗转换成了猫了 126 127 return new Cat(dog.name); 128 } 129 130 131 //委托 132 public void WagTail() 133 { 134 Console.WriteLine("狗狗摇尾巴"); 135 } 136 } 137 138 public class Labrador : Dog 139 { 140 public Labrador(string name) : base(name) 141 { 142 143 } 144 145 } 146 147 148 //扩展类 149 static class PetGuide 150 { 151 //this+类名A 大概就是 这个函数就像A里面的方法一样,这就是扩展类的含义咯 152 static public void HowToFeedDog(this Dog dog) // this+ 类名的写法 :特定写法 153 { 154 Console.WriteLine("-------------------"); 155 } 156 } 157 158 public class Cat : Pet 159 { 160 override public void Speek() 161 { 162 //base.Speek(); 163 Console.WriteLine("猫叫"); 164 } 165 166 //构造函数 167 public Cat(string name) : base(name) 168 { 169 } 170 171 //抽象方法 172 override public void Speek2() 173 { 174 Console.WriteLine("猫也会叫了"); 175 } 176 177 178 179 //隐式转换 180 public static explicit operator Dog(Cat cat) 181 { 182 return new Dog(cat.name); 183 } 184 185 //委托 186 public void Innocent() 187 { 188 Console.WriteLine("傻猫在发呆呢"); 189 } 190 } 191 192 193 class Program //类名 194 { 195 //委托 196 delegate void ActCute(); 197 198 199 static void Main(string[] args) 200 { 201 //多态 202 //Pet dog = new Dog(); //父类引用指向 子类对象 203 //dog.name = "Hello"; 204 //dog.printname(); 205 //dog.Speek(); 206 207 //Pet cat = new Cat(); 208 //cat.name = "World"; 209 //cat.printname(); 210 //cat.Speek(); 211 212 /////////////////////////////改进方法 213 Pet[] pets = new Pet[] { new Dog("Jack"), new Cat("Tom") }; //子类对象 被 父类对象引用 214 for (int i = 0; i < pets.Length; i++) 215 { 216 pets[i].printname(); 217 pets[i].Speek(); 218 pets[i].Speek2(); 219 } 220 221 222 223 Dog dog = new Dog("nima"); 224 dog.CatchMice(); 225 dog.ClimbTree(); 226 227 ICatchMice obj = (ICatchMice)dog; //子类对象转化成 接口对象 228 obj.CatchMice(); 229 230 IClimbTree obj2 = (IClimbTree)dog; //子类对象转化成 接口对象 231 obj2.ClimbTree(); 232 233 Dog.ShowAllDogNum();//通过类名直接调用静态函数 234 235 Dog dog3 = new Dog("dog3"); 236 dog3.HowToFeedDog();//使用扩展类的方法 237 238 239 240 //测试装箱 , 并且可以验证出这是2个独立的对象,互不影响 241 { 242 int i = 3; 243 object oi = i; //隐式转换 244 Console.WriteLine("{0},{1}", i, oi); 245 246 i = 4; 247 Console.WriteLine("{0},{1}", i, oi); 248 249 oi = 10; 250 Console.WriteLine("{0},{1}", i, oi); 251 252 } 253 254 //拆箱 255 { 256 int i = 3; 257 object oi = i; 258 259 int j = (int)oi; //显式转换 260 Console.WriteLine("{0},{1},{2}", i, oi, j); 261 262 263 i = 11; 264 oi = 22; 265 j = 33; 266 Console.WriteLine("{0},{1},{2}", i, oi, j); 267 } 268 269 270 271 272 273 { 274 //自定义转换 : 转换自定义的结构或类 275 276 Dog dog4 = new Dog("Jack"); 277 dog4.Speek(); 278 279 Cat cat4 = dog; //把dog类型转换成了cat类型 280 cat4.Speek(); 281 282 283 Dog dog5 = (Dog)cat4; 284 dog3.Speek(); 285 286 } 287 288 289 290 291 292 { 293 //集合 : 就相当于c++中的stl 294 295 296 //数组 297 List<Dog> list = new List<Dog>(); 298 list.Add(new Dog("A")); 299 list.Add(new Dog("B")); 300 list.Add(new Dog("C")); 301 302 list.RemoveAt(1); 303 for(int i=0;i<list.Count;++i) 304 { 305 306 list[i].printname(); 307 } 308 list.Clear(); 309 310 311 312 Console.WriteLine("map---------------"); 313 //map 314 Dictionary<string, Dog> dic = new Dictionary<string, Dog>(); 315 dic.Add("A",new Dog("A")); 316 dic.Add("B",new Dog("B")); 317 dic.Add("C",new Dog("C")); 318 319 dic["A"].printname(); 320 321 322 323 Console.WriteLine("栈---------------"); 324 //stack 栈 <特点: 先进后出,后进先出> 像羽毛球桶,一边开口 325 //出栈 就是出来 326 //入栈 就是进去 327 //栈顶 328 Stack<Pet> stack = new Stack<Pet>(); 329 stack.Push(new Dog("A")); 330 stack.Push(new Cat("B")); //进栈 331 332 stack.Peek().printname(); //获取栈顶信息 333 stack.Pop(); //出栈 334 stack.Peek().printname(); 335 336 337 Console.WriteLine("队列---------------"); 338 //队列 <特点: 先进先出> 自来水管,2头开口 339 Queue<Pet> queue = new Queue<Pet>(); 340 queue.Enqueue(new Dog("a")); //入队 341 queue.Enqueue(new Dog("b")); 342 queue.Enqueue(new Dog("c")); 343 344 Pet pet = null; 345 pet = queue.Dequeue(); //出队 346 pet.printname(); 347 pet = queue.Dequeue(); 348 pet.printname(); 349 pet = queue.Dequeue(); 350 pet.printname(); 351 352 } 353 354 355 356 357 358 { 359 Console.WriteLine("队列---------------"); 360 //委托 361 ActCute del = null; 362 Dog dog11 = new Dog("A"); 363 Cat cat11 = new Cat("B"); 364 365 del = dog11.WagTail; 366 del += cat11.Innocent; //这里是追加的操作,类似 c++的 <<操作符 367 368 369 del += () => 370 { 371 //Console.WriteLine("匿名方法---------------"); 372 // Console.WriteLine("lambada表达式---------------"); 373 Console.WriteLine("do nothing ...这特么的就是 匿名方法和lambada表达式的结合体?"); 374 }; 375 376 del(); 377 378 } 379 380 381 382 { 383 Console.WriteLine("事件---------------"); 384 //事件 385 //发布者:通知某件事情发生的,就是发布者 我发布了微博 386 //订阅者:对某事关注的,就是订阅者 我关注了微博 387 388 389 //事件触发和注册 390 //事件发生时,会通知所有关注该事件的订阅者 391 //想在事件发生时被通知,必须注册以表示关注 392 393 394 395 396 397 398 } 399 400 401 402 //Ctrl+F5 等于c++的 system("pause"); 403 Console.ReadKey();//这个也相当于是 system("pause"); 404 } 405 } 406 }