首页 > 代码库 > 虚方法 覆写
虚方法 覆写
using System; using System.Collections.Generic; using System.Linq; using System.Text; using graphlei;
namespace graphlei { /// <summary> /// 图形类 /// </summary> public class graph { protected string Color; public graph() { } public graph(string color) { this.Color = color; } public string GetColor() { return Color; } public virtual double Getgraph() { return 0.0; } } /// <summary> /// 圆类 /// </summary> class round : graph { double Nameone; public round() { } public round(string color, double nameone) { this.Color = color; this.Nameone = nameone; } /// <summary> /// 面积 /// </summary> /// <returns></returns> public override double Getgraph() { return System.Math.PI * Nameone * Nameone; } } /// <summary> /// 矩形类 /// </summary> /// class rectangle : graph { protected double Lenght, Width; public rectangle() { } public rectangle(string color, double Lenght, double Width) { this.Color = color; this.Lenght = Lenght; this.Width = Width; } /// <summary> /// 面积 /// </summary> /// <returns></returns> public override double Getgraph() { return Lenght * Width; } /// <summary> /// 周长 /// </summary> /// <returns></returns> public double Getrectangle() { return (2 * (Lenght + Width)); } }
/// <summary> /// 正方形类 /// </summary> /// class square : rectangle { protected double side; public square() { } public square(string Color, double side) { this.Color = Color; this.side = side; } public override double Getgraph() { return side * side; } public double Getsquare() { return 2 * (side + side); } } } class Program { static void Main(string[] args) { round Myround = new round("红色", 2); Console.WriteLine("圆颜色 {0},圆的面积 {1}", Myround.GetColor(), Myround.Getgraph()); Console.ReadKey(); rectangle Myrectangle = new rectangle("红色", 1, 2); Console.WriteLine("矩形颜色 {0},矩形面积{1},矩形的周长{2}", Myrectangle.GetColor(), Myrectangle.Getgraph(), Myrectangle.Getrectangle()); Console.ReadKey(); square Mysquare = new square("红色", 5); Console.WriteLine("正方形的颜色{0},正方形的面积{1},正方形的周长{2}",Mysquare.GetColor(), Mysquare.Getgraph(), Mysquare.Getsquare()); Console.ReadKey(); }
}