上官玉� 2021-03-10 20:44 采纳率: 100%
浏览 64
已采纳

C#,抽象类和抽象方法,关于多态的应用

有一种立体图形叫直棱柱,直棱柱的上下底面可以是圆形、三角形、四边形等。

(1)创建一个直棱柱抽象类RightPrism(构造方法为Shape类和double型高),以及抽象方法(计算体积GetVolume,计算侧面积GetLateralArea )。

(2)发挥面向对象的思想,封装出圆柱体(Cylinder)、三棱柱(TrianglarPrism)、正方体(Cube)、长方体(Cuboid)对应的类。

(3)创建各个形状的实例,在控制台中打印出各种形状的侧面积(LateralArea)和体积(Volume)。

  • 写回答

2条回答 默认 最新

  • Go 旅城通票 2021-03-10 21:48
    关注

     

    using System;
    
    namespace RightPrism
    {
        public abstract class Shape
        {
            public abstract double GetArea();
            public abstract double GetCircumference();
        }
        public class Circle : Shape
        {
            public double x { get; set; }
            public override double GetArea()
            {
                return Math.Round(Math.PI * Math.Pow(x, 2), 4);
            }
            public override double GetCircumference()
            {
                return Math.Round(2 * x * Math.PI, 4);
            }
        }
        public class Rectangle : Shape
        {
            public double x { get; set; }
            public double y { get; set; }
            public Rectangle(double x, double y)
            {
                this.x = x; this.y = y;
            }
            public override double GetArea()
            {
                return x * y;
            }
            public override double GetCircumference()
            {
                return (x + y) * 2;
            }
        }
        public class Square : Rectangle { public Square(double x) : base(x, x) { } }
    
        public class Triangle : Shape
        {
            public double x { get; set; }
            public double y { get; set; }
            public double z { get; set; }
            public Triangle(double x, double y, double z)
            {
                if (x + y > z && x + z > y && y + z > x)
                {
                    this.x = x; this.y = y; this.z = z;
                }
                else Console.WriteLine(x + "," + y + "," + z + "无法构成三角形!");
            }
            public override double GetArea()
            {
                double p = (x + y + z) / 2;
                return Math.Round(Math.Sqrt((p * (p - x) * (p - y) * (p - z))), 4);//保留4位小数,保留位数改这里
            }
            public override double GetCircumference()
            {
                return x + y + z;
            }
        }
    
        public abstract class RightPrism
        {
            public abstract double GetVolume();
            public abstract double GetLateralArea();
            public Shape p { get; set; }
            public double height { get; set; }
            public RightPrism(Shape p, double height)
            {
                this.p = p;
                this.height = height;
            }
        }
        public class Cylinder : RightPrism
        {
            public Cylinder(Shape p, double height) : base(p, height) { }
            public override double GetLateralArea() { return Math.Round(p.GetCircumference() * height, 4); }
            public override double GetVolume() { return Math.Round(p.GetArea() * height, 4); }
        }
        public class TrianglarPrism : RightPrism
        {
            public TrianglarPrism(Shape p, double height) : base(p, height) { }
            public override double GetLateralArea() { return Math.Round(p.GetCircumference() * height, 4); }
            public override double GetVolume() { return Math.Round(p.GetArea() * height, 4); }
        }
        public class Cube : RightPrism
        {
            public Cube(Shape p, double height) : base(p, height) { }
            public override double GetLateralArea() { return Math.Round(p.GetCircumference() * height, 4); }
            public override double GetVolume() { return Math.Round(p.GetArea() * height, 4); }
        }
        public class Cuboid : RightPrism
        {
            public Cuboid(Shape p, double height) : base(p, height) { }
            public override double GetLateralArea() { return Math.Round(p.GetCircumference() * height, 4); }
            public override double GetVolume() { return Math.Round(p.GetArea() * height, 4); }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                RightPrism o = new Cylinder(new Circle { x = 5 }, 5);
                Console.WriteLine(String.Format("圆柱体:{0,-15}{1}", "侧面积:" + o.GetLateralArea(), "体积:" + o.GetVolume())); 
                o = new TrianglarPrism(new Triangle (3,4,5), 5);
                Console.WriteLine(String.Format("三棱柱:{0,-15}{1}", "侧面积:" + o.GetLateralArea(), "体积:" + o.GetVolume()));
                o = new Cube(new Square(3), 5);
                Console.WriteLine(String.Format("正方体:{0,-15}{1}", "侧面积:" + o.GetLateralArea(), "体积:" + o.GetVolume()));
                o = new Cube(new Rectangle(3,4), 5);
                Console.WriteLine(String.Format("长方体:{0,-15}{1}", "侧面积:" + o.GetLateralArea(), "体积:" + o.GetVolume()));
                Console.ReadKey();
            }
        }
        
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化