1.声明一个图形基类Shape,该类拥有用于存储Shape面积的属性S,以及用于计算面积的虚方法GetArea,由Shape类派生出Rectangle和Circle类,并在子类中重写GetArea方法

5 篇文章 0 订阅
1 篇文章 0 订阅

1.声明一个图形基类Shape,该类拥有用于存储Shape面积的属性S,以及用于计算面积的虚方法GetArea,由Shape类派生出Rectangle和Circle类,并在子类中重写GetArea方法分别用于计算矩形和圆形的面积,在主函数中完成派生类的实例化及其测试。

2.基类Shape类是一个表示形状的抽象类,该类拥有用于存储Shape面积的属性Area,以及用于求面积的抽象方法GetArea。请从Shape类派生三角形类(),并给出具体的求面积函数GetArea,在主函数中完成类的实例化及其测试。

```c
namespace 
{
    public class Shape
    {
        private float s;
        public float S
        {
            get
            {
                return s;
            }
            set
            {
                s = value;
            }

        }
        public virtual void GetArea()
        {

        }
    }
    public class Rectangle : Shape
    {               
                public float x;
                public float y;         
        public override void GetArea()
        {
            Console.WriteLine("输入矩形长和宽");
            float x = float.Parse(Console.ReadLine());
            float y = float.Parse(Console.ReadLine());     
            this.S = x * y;
        }
    }


    class Circle : Shape
    {
        static float pi = 3.1415926f;
        public float r;
        public override  void GetArea()
        {
            Console.WriteLine("输入圆的半径r:");
            float r = float.Parse(Console.ReadLine());
            this.S =pi * r * r;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Rectangle rectangle = new Rectangle();
            rectangle.GetArea();
            Console.WriteLine("矩形的面积:{0}",rectangle.S);

            Circle circle = new Circle();
            circle.GetArea();
           Console.WriteLine("圆形的面积:{0}", circle.S);
        }
    }
}

 

```c
namespace 作业3_2
{


    abstract class Shape
    {
        public double s;
        public abstract void GetArea();
        public  double Area { get; set; }
    }
        class Triangle : Shape
        {
            
            
            public override void  GetArea()
            {
                Console.WriteLine("输入三角形底和高");
                float h = float.Parse(Console.ReadLine());
                float d = float.Parse(Console.ReadLine());            
                this.Area = h * d * 1 / 2;
                      
            }
            public new double Area
            {
                get
                {
                    return s;
                }
                set
                {
                    s = value;
                }
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Triangle triangle = new Triangle();
                triangle.GetArea();
            Console.WriteLine("三角形的面积:{0}", triangle.Area);
        }
        }
    
}

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值