0
点赞
收藏
分享

微信扫一扫

基类Shape类是一个表示形状的抽象类,该类拥有用于存储Shape面积的属性Area,以及用于求面积的抽象方法GetArea。请从Shape类派生三角形类(Triangle),并给出具体的求面积函数G

棒锤_45f2 2022-04-21 阅读 52
c#
abstract class Shape
   {
       private float Area;
       public float area
       {
           get
           {
               return Area;
           }
           set
           {
               if (value >= 0)
               {
Area = value;
}
           }
       }
   }
   class Triangle : Shape
   {
       public float GetArea(float s, float h)
       {
           return area = s * h / 2;
       }
   }
   class Program
   {
       static void Main(string[] args)
       {
         float s, h;
         Triangle triangle = new Triangle();
         Console.WriteLine("请输入三角形的底和高:");
       
s = float.Parse(Console.ReadLine());
        h = float.Parse(Console.ReadLine());
        Console.WriteLine("三角形的面积={0}", triangle.GetArea(s, h));
       }
   }
举报

相关推荐

0 条评论