突然对C#充满了兴趣,于是兴致勃勃的折腾起来。
下面是折腾的过程:
IDE,我选择了vs2017,不过这货的安装选项略多,对于新手来说各种坑。无论如何,选择”.NET桌面开发”即可,其他的全部留空。
然后新建项目,C#项目,控制台项目。
可以开搞了!
总的来说,C#和Java很像 -- 抽象、虚函数、继承、接口等,然而又具有C++的很多特性,如运算符重载!
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Go { static void Main(string[] args) { Cat cat1 = new Cat("a_cat"); Cat cat2 = new Cat("b_cat"); Cat cat3 = new Cat("c_cat"); Console.WriteLine(Animal.Count); Console.WriteLine(cat1); //测试委托 DoIt doIt = cat1.Speak; doIt += cat2.Speak; //注意,只要方法签名一致即可连续添加 doIt += cat3.Speak; doIt += () => { Console.WriteLine("来自Lambda"); };//lambda doIt(); //测试静态类方法扩展 Dog dog = new Dog("a_dog"); dog.Jump();//这个方法,类似于js的apply或者call //测试隐式转换 Cat cat = dog; Console.WriteLine(cat); //测试显式转换 dog = (Dog)cat; Console.WriteLine(dog); //模拟事件 Client c1 = new Client(); Client c2 = new Client(); Animal.NewAnimal += c1.WantAnimal; Animal.NewAnimal += c2.WantAnimal; Animal dogq = new Dog("Q-dog"); //测试集合 ArrayList al = new ArrayList(); al.Add(cat1); al.Add(cat2); al.Add(cat3); al.Add(cat2); Console.WriteLine("ArrayList‘s count is "+al.Count); al.Remove(cat2); //只会删除第一个 Console.WriteLine("ArrayList‘s count is " + al.Count); List<Animal> list = new List<Animal>(); list.Add(cat1); list.Add(cat2); list.Add(cat3); list.Add(cat2); Console.WriteLine("List‘s count is " + list.Count); Stack<Animal> stack = new Stack<Animal>(); stack.Push(cat1); stack.Push(cat2); stack.Push(cat3); Console.WriteLine("stack.Peek() = " + stack.Peek() + "; stack.Count = " + stack.Count); Console.WriteLine("stack.Pop() = " + stack.Pop() + "; stack.Count = " + stack.Count); Queue<Animal> queue = new Queue<Animal>(); queue.Enqueue(cat1); queue.Enqueue(cat2); queue.Enqueue(cat3); Console.WriteLine("queue.First() = " + queue.First() + "; queue.Count = " + queue.Count); Console.WriteLine("queue.Last() = " + queue.Last() + "; queue.Count = " + queue.Count); Console.WriteLine("queue.Dequeue() = " + queue.Dequeue() + "; queue.Count = " + queue.Count); Dictionary<int, Animal> dic = new Dictionary<int, Animal>(); dic.Add(1, cat1); dic.Add(2, cat2); dic.Add(3, cat3); Console.WriteLine("dic.Keys = " + dic.Keys + "; dic.Count = " + dic.Count); Console.WriteLine("自定义扩展dic.Print() = " + dic.PrintKeys() + "; dic.Count = " + dic.Count); Console.WriteLine("自定义扩展dic.PrintValues() = " + dic.PrintValues() + "; dic.Count = " + dic.Count); Console.WriteLine(dic); Console.ReadLine(); } } public delegate void DoIt();//委托,托管一堆方法?!位置,放这里也行? interface ISpeak { void Speak(); } interface IEat { void Eat(); } class Animal { protected string _name; public static int Count;//用来计数,数一下有多少animal的实例 public delegate void Handler();//事件订阅 public static event Handler NewAnimal; //事件 public Animal() { ++Count; //通知各位,有新动物来啦 NewAnimal?.Invoke(); } public Animal(string Name):this() { _name = Name; } static Animal() { Count=0; } public void setName(string Name) { _name = Name; } public string getName() { return _name ; } override public string ToString() //重写父类的 { return "Animal[_name="+_name+", Count="+Count+"]"; } } //狗类,继承自动物,实现接口 class Dog : Animal, ISpeak, IEat { public Dog(string Name) : base(Name) { } public void Eat() { Console.WriteLine("我是一只狗,名字:["+base._name+"],我在吃狗粮!"); } public void Speak() { Console.WriteLine("我是一只狗,名字:[" + base._name + "],汪汪汪!"); } //定义一个隐式转换 public static implicit operator Cat(Dog dog) { return new Cat(dog._name);//new 会改变Animal.Count } } //猫类,继承自动物,实现接口 class Cat : Animal, ISpeak, IEat { public Cat(string Name) : base(Name) { } public void Eat() { Console.WriteLine("我是一只猫,名字:[" + base._name + "],我在吃小鱼干~"); } public void Speak() { Console.WriteLine("我是一只猫,名字:[" + base._name + "],喵~"); } //定义一个显式转换 public static explicit operator Dog(Cat cat) { return new Dog(cat._name);//new 会改变Animal.Count } } //静态类 扩展方法 static class DogJump { public static void Jump(this Dog dog) { Console.WriteLine(dog + " is jumping..."); } public static string PrintKeys(this Dictionary<int, Animal> dic) { var s=""; foreach (var item in dic.Keys) { s += item; s += ", "; } return s; } public static string PrintValues(this Dictionary<int, Animal> dic) { var s = ""; foreach (var item in dic.Values) { s += item; s += ", "; } return s; } } //定义一个客户类 class Client { public void WantAnimal() { Console.WriteLine("Great! I wanna see it!"); } } }
时间: 2024-10-07 17:11:48