• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
胡卫军
博客园    首页    新随笔    联系   管理    订阅  订阅

虚方法 覆写

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();         }

    }

 

posted @ 2014-07-31 09:21  胡卫军  阅读(111)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3