C#类的多态性:求图形的面积

1)、代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace shap
{
    public class shap
    {
        public virtual void MyArea()
        {
            Console.WriteLine("no use");
        }
    }
    public class circle : shap
    {
        double r, carea;
        public circle(double r)
        {
            this.r = r;
        }
        public override void MyArea()
        {
            carea = Math.PI * r * r;
            Console.WriteLine("该圆形的面积为{0}", carea);
        }
    }
    public class triangle : shap
    {
        double tarea, hemiline, h;
        public triangle(double hemiline, double h)
        {
            this.hemiline = hemiline;
            this.h = h;
        }
        public override void MyArea()
        {
            tarea = hemiline * h / 2;
            Console.WriteLine("hemiline={0},h={1}", hemiline, h);
            Console.WriteLine("该圆形的面积为{0}", tarea);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine("请输入你选择你要计算的图形类型:");
                Console.WriteLine("1.circle(圆形面积)");
                Console.WriteLine("2.triangle(三角形的面积)");
                int s1 = Convert.ToInt32(Console.ReadLine());
                switch (s1)
                {
                    case 1:
                        Console.WriteLine("请输入圆的半径r:");
                        int r;
                        r = Int32.Parse(Console.ReadLine());
                        shap MyShape;
                        MyShape = new circle(r);
                        MyShape.MyArea();
                        break;
                    case 2:
                        Console.WriteLine("请输入三角形的高h,宽íhemiline:");
                        int h, hemiline;
                        h = Int32.Parse(Console.ReadLine());
                        hemiline = Int32.Parse(Console.ReadLine());
                        MyShape = new triangle(hemiline, h);
                        MyShape.MyArea();
                        break;
                    default:
                        Console.WriteLine("您输入有误,请重新输入!");
                        break;
                }

            }

        }
    }
}

2)、实验结果截图:

这里写图片描述

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个示例代码: ``` using System; namespace GeometryCalculator { class Program { static void Main(string[] args) { // 创建各种立体图形的实例 Cube cube = new Cube(5); Sphere sphere = new Sphere(3); Pyramid pyramid = new Pyramid(4, 6); Cylinder cylinder = new Cylinder(2, 5); // 计算并输出各种立体图形的体积和表面积 Console.WriteLine("Cube: Volume={0}, Surface Area={1}", cube.Volume(), cube.SurfaceArea()); Console.WriteLine("Sphere: Volume={0}, Surface Area={1}", sphere.Volume(), sphere.SurfaceArea()); Console.WriteLine("Pyramid: Volume={0}, Surface Area={1}", pyramid.Volume(), pyramid.SurfaceArea()); Console.WriteLine("Cylinder: Volume={0}, Surface Area={1}", cylinder.Volume(), cylinder.SurfaceArea()); Console.ReadLine(); } } // 抽象的立体图形 abstract class Shape { // 计算体积的抽象方法 public abstract double Volume(); // 计算面积的抽象方法 public abstract double SurfaceArea(); } // 立方体 class Cube : Shape { private double sideLength; public Cube(double sideLength) { this.sideLength = sideLength; } public override double Volume() { return Math.Pow(sideLength, 3); } public override double SurfaceArea() { return 6 * Math.Pow(sideLength, 2); } } // 球体 class Sphere : Shape { private double radius; public Sphere(double radius) { this.radius = radius; } public override double Volume() { return (4 / 3.0) * Math.PI * Math.Pow(radius, 3); } public override double SurfaceArea() { return 4 * Math.PI * Math.Pow(radius, 2); } } // 三棱锥 class Pyramid : Shape { private double baseLength; private double height; public Pyramid(double baseLength, double height) { this.baseLength = baseLength; this.height = height; } public override double Volume() { return (1 / 3.0) * Math.Pow(baseLength, 2) * height; } public override double SurfaceArea() { double slantHeight = Math.Sqrt(Math.Pow(baseLength / 2.0, 2) + Math.Pow(height, 2)); return (baseLength * slantHeight) + (2 * Math.Pow(baseLength / 2.0, 2)); } } // 柱体 class Cylinder : Shape { private double radius; private double height; public Cylinder(double radius, double height) { this.radius = radius; this.height = height; } public override double Volume() { return Math.PI * Math.Pow(radius, 2) * height; } public override double SurfaceArea() { return (2 * Math.PI * radius * height) + (2 * Math.PI * Math.Pow(radius, 2)); } } } ``` 以上代码中,我们定义了一个抽象的立体图形 `Shape`,其中包含了两个抽象方法 `Volume()` 和 `SurfaceArea()`,用于计算体积和表面积。然后我们分别定义了立方体 `Cube`、球体 `Sphere`、三棱锥 `Pyramid` 和柱体 `Cylinder`,这些都继承自 `Shape` ,并分别实现了其抽象方法。在 `Main` 方法中,我们创建了各种立体图形的实例,并调用了它们的 `Volume()` 和 `SurfaceArea()` 方法来计算它们的体积和表面积,并输出结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值