zoukankan      html  css  js  c++  java
  • 用面向对象多态的思想分别去求圆形和长方形的面积和周长

    //用面向对象多态的思想分别去求圆形和长方形的面积和周长
    
    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;
                }
            }
  • 相关阅读:
    python 列表、元组、字典总结
    python 字典
    python 元组
    python 列表
    JMeter的作用域与执行顺序
    JMeter第一个实战
    JMeter录制的两种方法
    JMeter常用功能
    初识jmeter2
    handler的拒绝策略:
  • 原文地址:https://www.cnblogs.com/kangshuai/p/4705088.html
Copyright © 2011-2022 走看看