标签:
当我们处理简单对象和复杂对象时,如果他们有共性,那么我们可以使用统一接口,让用户更好的操作不同的对象。
using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace ConsoleApplication1 { class Program { public abstract class Shape { private string name; public Shape(string name) { this.name = name; } public abstract double Area(); public abstract void Display(); } public class Circle : Shape { private double radius; public Circle(string name, double radius) : base(name) { this.radius = radius; } public override double Area() { return Math.Round(Math.PI*radius*radius); } public override void Display() { Console.WriteLine("面积 : " + Area()); } } public class Line : Shape { public Line(string name) : base(name) { } public override double Area() { Console.WriteLine("can not calculat area"); return 0; } public override void Display() { Console.WriteLine("can not display area"); } } static void Main(string[] args) { Circle circle = new Circle("circle", 5f); circle.Display(); Line line = new Line("line"); line.Display(); } } }
标签:
原文地址:http://www.cnblogs.com/zkzk945/p/5114075.html