构造函数
class Player{public int id = 0;public string name = \"\";public int atk = 10;public int hp = 10;public Player(){name = \"川普\";}public Player(string name){this.name = name;Console.WriteLine(\"一个参数的构造\");}public Player(string name,int atk):this(name){this.atk = atk;Console.WriteLine(\"两个参数的构造\");}public Player(string name,int atk,int hp):this(name,atk){this.hp = hp;Console.WriteLine(\"三个参数的构造\");}static void Main(string[] args){Player player = new Player(\"王小虎\",20,100);}}
继承
class A{public string name;public int atk = 10;public int hp = 10;}class B:A{public int satk = 5;public int miss = 5;public B(){name = \"aaa\";}}class C{static void Main(string[] args){B b = new B();b.hp = 5;b.name = \"haha\";b.atk = 10;}}
构造与继承
class A{public string name;public int atk = 10;public int hp = 10;public A(string name,int hp,int atk){this.name = name;this.hp = hp;this.atk = atk;}}class B:A{public int satk = 5;public int miss = 5;public B(string name, int hp, int atk,int satk,int miss):base(name,hp,atk){this.satk = satk;this.miss = miss;}}
如果B类不想继承A类的构造函数还想要独自构造函数,可以在A类中添加public A(){}
子类向父类可以隐式转换
父类向子类必须强制转换
可以拿父类的变量接收子类的实体,反之不行。
多态
方法的覆盖(知道就行)
class A{public string name;public void Attack(){Console.WriteLine(\"a开始攻击\");}}class B : A{public int atk;public new void Attack(){Console.WriteLine(\"b开始攻击\");}}
方法的重写
class A{public virtual void Attack(){Console.WriteLine(\"啊啊啊\");}}class B : A{public override void Attack(){//base.Attack();Console.WriteLine(\"吼吼吼\");}}
多态的体现
class A{public virtual void Attack(){Console.WriteLine(\"啊啊啊\");}}class B : A{public override void Attack(){//base.Attack();Console.WriteLine(\"吼吼吼\");}}class C : A{public override void Attack(){//base.Attack();Console.WriteLine(\"喵喵喵\");}}class D : A{public override void Attack(){//base.Attack();Console.WriteLine(\"呜呜呜\");}}class M{static void Main(string[] args){A a1 = new B();A a2 = new C();A a3 = new D();a1.Attack();a2.Attack();a3.Attack();}}
最后输出三种叫声。