首页 > 其他 > 详细

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

时间:2015-08-05 17:41:15      阅读:455      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!