#region 多态继承 public abstract class animal { public void Eat() { Console.WriteLine("animal Eat"); } public virtual void Call() { Console.WriteLine("animal Call"); } public abstract void live(); } public class cat : animal { public override void Call() { Console.WriteLine("cat Call"); } public override void live() { Console.WriteLine("the cat is live"); } } public class dog : animal { public void Eat() { Console.WriteLine("dog Eat"); } public override void live() { Console.WriteLine("the dog is live"); } } #endregion
多态。。
重写,重载,abstract,override,virtual
abstract :无实体函数,子类必须实现
virtual:有实体函数,子类可以不实现,如果实现实为覆盖;
如果不标注virtual的函数被子类实现了,那么子类的函数与父类并存,且实现的时候不需要加override修饰
#region 多态继承 public abstract class animal { public void Eat() { Console.WriteLine("animal Eat"); } public virtual void Call() { Console.WriteLine("animal Call"); } } public class cat : animal { public override void Call() { Console.WriteLine("cat Call"); } } public class dog : animal { public void Eat() { Console.WriteLine("dog Eat"); } } #endregion
这个代码是.NET传参数的方式,ref,out,params
REF:传参数前先赋值
OUT:返回前必须赋值
params:无限制的传参数,随便多少个都无所谓
public class CloneString : ICloneable { public int intI = 1234; public Car aCar = new Car(); public object Clone() { CloneString cs= new CloneString(); cs.intI = intI; cs.aCar.CarName = aCar.CarName; cs.aCar.CarNum = aCar.CarNum; return cs; } public object MemberwishClone() { return base.MemberwiseClone(); } public override string ToString() { return "CarNum:" + aCar.CarNum + "aCar.CarName:" + aCar.CarName; } } public class Car { public string CarName = "hongqi"; public int CarNum = 10010 ; }
深复制。复制引用类型和值类型
//这个类测试属性 public class student { protected string _name = ""; public string Name { set { _name = value; } get { if (_name == "wsenmin") return _name + " you are best student!"; else return _name + " you are not wsenmin !"; } } }
属性,方便代码扩展,让字段不暴露
public class people { protected string _name = ""; protected int _age = 0; public people(string name, int age) { Name = name; Age = age; } public string Name { set { _name = value; } get { return _name; } } public int Age { set { _age = value; } get { return _age; } } public string SayHello() { return Name + "对你说 “你好”"; } } /// <summary> /// 索引测试 /// </summary> public class PeopleS { people[] ps = new people[4]; public PeopleS() { ps[0] = new people("a", 1); ps[1] = new people("b", 132); ps[2] = new people("c", 32); ps[3] = new people("wsenmin", 27); } public people this[int index] { get { return ps[index]; } } public people this[string name] { get { for (int i = 0; i < ps.Length; i++) { if (name == ps[i].Name) { return ps[i]; } } return null; } } }
索引器,很简单的查找和匹配
using:类似于try catch finally ,自动调用dispose方法