using System; using System.Collections.Generic; using System.Text; namespace Comp ... { class Program ...{ static void Main(string[] args) ...{ //定义泛型 存放Computer类型的数据 List<Computer> computer = new List<Computer>(); NormalComputer c1 = new NormalComputer("aaa", "Intel", "立式"); NormalComputer c2 = new NormalComputer("bbb", "AMD", "卧式"); NotebookComputer c3 = new NotebookComputer("ccc", "Intel2G", 2000); NotebookComputer c4 = new NotebookComputer("ddd", "AMD3000+", 5000); computer.Add(c1); computer.Add(c2); computer.Add(c3); computer.Add(c4); foreach (Computer c in computer) ...{ c.show(); Console.WriteLine(); } } } /**//// <summary> /// 定义抽象类Computer /// </summary> abstract class Computer ...{ string type; string CPU; protected Computer(string type, string CPU) ...{ this.type = type; this.CPU = CPU; } /**//// <summary> /// 虚方法 /// </summary> public virtual void show() ...{ Console.WriteLine("类型:" + type); Console.WriteLine("CPU:" + CPU); } } /**//// <summary> /// 定义NormalComputer继承自Computer /// </summary> class NormalComputer : Computer ...{ string t; /**//// <summary> /// 调用基类的构造方法来初始化子类的参娄CPU /// </summary> /// <param name="type"></param> /// <param name="CPU"></param> /// <param name="t"></param> public NormalComputer(string type, string CPU, string t) : base(type, CPU) ...{ this.t = t; } /**//// <summary> /// 重写基类构造方法 /// </summary> public override void show() ...{ base.show(); Console.WriteLine("机箱类型:" + t); } } class NotebookComputer : Computer ...{ int capacity; public NotebookComputer(string type, string CPU, int capacity) : base(type, CPU) ...{ this.capacity = capacity; } public override void show() ...{ base.show(); Console.WriteLine("电池容量:" + capacity); } }}