抽象类与抽象成员
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 抽象类与抽象成员 { abstract public class CUnit { private string myName; private int mySpeed; public CUnit() { myName = ""; mySpeed = 0; } public CUnit(string name, int speed) { myName = name; mySpeed = speed; } public string Name { get { return myName; } set { myName = value; } } public int Speed { get { return mySpeed; } set { mySpeed = value; } } abstract public void Move(int x, int y); } public class CSoldier : CUnit { public CSoldier() { this.Name = "士兵"; this.Speed = 10; } //如果子类成员有和父类成员同名的时候,就必须用this和base区分。 //父类里面有一个成员比如int a;那么你子类里面也可以再定义一个int a, //这个时候base.a和this.a表示的就不是一个变量了,如果说子类里面没有, //那么base.a和this.a表示的都是一个变量。 public CSoldier(string name, int speed): base(name, speed) { } public override void Move(int x, int y) { Console.WriteLine("{0}步行到({1},{2})", this.Name, x, y); } } public class CJeep : CUnit { public CJeep() { this.Name="吉普"; this.Speed = 100; } public CJeep(string name, int speed): base(name, speed) { } public override void Move(int x, int y) { Console.WriteLine("{0}行驶到({1},{2})",this.Name,x,y); } } class Program { static void Main(string[] args) { CSoldier cs=new CSoldier(); cs.Move(4,7); CJeep cj=new CJeep(); cj.Move(50, 80); } } }