码迷,mamicode.com
首页 > 其他好文 > 详细

用面向对象多态的思想分别去求圆形和长方形的面积和周长

时间:2015-08-05 17:43:36      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

//用面向对象多态的思想分别去求圆形和长方形的面积和周长

static void Main(string[] args)
        {  
            Sharp sharp = new Circle(5);
            double area=sharp.GetArea();
            double perimeter= sharp.Getperimeter();
            Console.WriteLine("面积是{0},周长是{1}",area,perimeter);
            Console.ReadKey();
        }
        public abstract class Sharp
        {
            public abstract double  GetArea();
            public abstract double  Getperimeter();
        }
        public class Circle : Sharp
        {
            private double _r;
            public double R
            {
                get { return _r; }
                set { _r = value; }
            }
            public Circle(double r)
            {
                this.R = r;
            }
            public override double GetArea()
            {
                return Math.PI * this.R * this.R;
            }
            public override double Getperimeter()
            {
                return 2 * Math.PI * this.R;
            }
        }
        public class Square : Sharp
        {
            private double _height;
            public double Height
            {
                get { return _height; }
                set { _height = value; }
            }
            private double _width;
            public double Width
            {
                get { return _width; }
                set { _width = value; }
            }
            public Square(double height, double width)
            {
                this.Height = height;
                this.Width = width;
            }
            public override double GetArea()
            {
                return this.Height * this.Width;
            }
            public override double Getperimeter()
            {
                return (this.Height + this.Width)*2;
            }
        }

 

用面向对象多态的思想分别去求圆形和长方形的面积和周长

标签:

原文地址:http://www.cnblogs.com/kangshuai/p/4705088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!