注意修饰符,父类的的抽象类和抽象方法的修饰符合class一起修饰的,子类实现父类的方法也带有“override”关键字
父类
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp1 { /// <summary> /// 蔬菜类 /// </summary> abstract class Vegetables { /// <summary> /// 蔬菜的颜色 /// </summary> public abstract void colour(); } }
子类
using System; using System.Collections; using System.Collections.Generic; using System.Text; namespace ConsoleApp1 { /// <summary> /// 胡萝卜类 /// </summary> class Carrot : Vegetables { public static void Main(string[] args) { Carrot c = new Carrot(); c.colour(); } public override void colour() { Console.WriteLine("实现父类的方法因为父类是抽象的"); } } }