C#上机实验之题目三

题目三


分析:


       分别使用三种方法实现

  1. 类的继承与虚函数,即多态性;
  2. 抽象类继承;
  3. 接口。


第一种方法,代码如下:

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

namespace Shangji1_03
{
    public class Circle //基类
    {
        protected double radius;
        public Circle(double r) { radius = r; }
        public virtual double GetArea()
        { return Math.PI * radius * radius; }
        public virtual double GetVolumn() { return 0.0; }
    }
    public class Sphere : Circle//球体类
    {
        public Sphere(double r) : base(r) { }
        public override double GetArea()
        { return (4 * base.GetArea()); }
        public override double GetVolumn()
        { return (4 * Math.PI * Math.Pow(radius, 3) / 3); }
    }
    public class Cylinder : Circle//圆柱类
    {
        private double height;//添加高度字段
        public Cylinder(double r, double h) : base(r) { height = h; }
        public override double GetArea()
        { return (2 * base.GetArea() + 2 * Math.PI * radius * height); }
        public override double GetVolumn()
        { return (Math.PI * radius * radius * height); }
    }
    public class Cone : Circle//圆锥类
    {
        private double height;//添加高度字段
        public Cone(double r, double h) : base(r) { height = h; }
        public override double GetArea()
        {
            return (Math.PI * radius *
                    (radius + Math.Sqrt(height * height + radius * radius)));
        }
        public override double GetVolumn()
        { return (Math.PI * radius * radius * height / 3); }

    }


    class Program
    {
        static void Main(string[] args)
        {
            Circle[] c = new Circle[3];//建立基类数组
            c[0] = new Sphere(2);
            c[1] = new Cylinder(2, 10);
            c[2] = new Cone(2, 20);
            for (int i = 0; i < 3; ++i)
            {
                Console.WriteLine("c[{0}]’s surface area={1}, volumn={2}",
                                             i, c[i].GetArea(), c[i].GetVolumn());
            }
            Console.ReadKey();
        }
    }
}


第二种方法,代码如下:

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

namespace Shangji1_03
{
    public abstract class Shape
    {
        public const double PI = 3.1415926;
        public abstract double getArea();  //求面积
        public abstract double getVol();  //求体积
    }
    public class Sphere : Shape
    {
        private double r;  //半径

        public Sphere(double r)
        {
            // TODO: Complete member initialization
            this.r = r;
        }
        public override double getArea()
        {
            double area = 0;
            area = 4*PI * r * r;
            return area;
        }
        public override double getVol()
        {
            double vol = 0;
            vol = 4 / 3 * PI * r * r * r;
            return vol;
        }
    }
    public class Cylinder : Shape
    {
        private double r;  //底面半径
        private double h;  //圆柱的高

        public Cylinder(double r, double h)
        {
            // TODO: Complete member initialization
            this.r = r;
            this.h = h;
        }
        public override double getArea()
        {
            double area;
            area = 2 * PI * r * r + 2 * PI * r * h;
            return area;
        }
        public override double getVol()
        {
            double vol;
            vol = PI * r * r * h;
            return vol;
        }
    }
    public class Cone : Shape
    {
        private double r;  //底面半径
        private double l;  //圆锥的母线

        public Cone(double r, double l)
        {
            // TODO: Complete member initialization
            this.r = r;
            this.l = l;
        }
        public override double getArea()
        {
            double area;
            area = PI*r*r+PI*r*l;
            return area;
        }
        public override double getVol()
        {
            double vol;
            vol = 1 / 3.0d * PI * r * r * Math.Sqrt(l * l - r * r);
            return vol;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            double r = 3.0d, h = 4.0d, l = 5.0d;
            Shape shape = new Sphere(r);
            Console.WriteLine("球的表面积为:" + shape.getArea().ToString() + ";体积为:" + shape.getVol().ToString());
            shape = new Cylinder(r,h);
            Console.WriteLine("圆柱的表面积为:" + shape.getArea().ToString() + ";体积为:" + shape.getVol().ToString());
            shape = new Cone(r,l);
            Console.WriteLine("圆锥的表面积为:" + shape.getArea().ToString() + ";体积为:" + shape.getVol().ToString());
            Console.ReadKey();
        }
    }
}


第三种方法,代码如下:

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

namespace Shangji1_03
{
    public interface ICalAreaAndVolumn //声明接口
    {
        double getArea();
        double getVolumn();
    }
    public class Sphere : ICalAreaAndVolumn //球类
    {
        private double radius;
        public Sphere(double r) { radius = r; }
        public double getArea()
        { return (4 * Math.PI * radius * radius); }
        public double getVolumn()
        { return (4 * Math.PI * Math.Pow(radius, 3) / 3); }
    }
    public class Cylinder : ICalAreaAndVolumn //圆柱类
    {
        private double radius, height;
        public Cylinder(double r, double h) { radius = r; height = h; }
        public double getArea()
        { return (2 * Math.PI * radius * radius + 2 * Math.PI * radius * height); }
        public double getVolumn()
        { return (Math.PI * radius * radius * height); }
    }
    public class Cone : ICalAreaAndVolumn //圆锥类
    {
        private double radius, height;
        public Cone(double r, double h) { radius = r; height = h; }
        public double getArea()
        {
            return (Math.PI * radius * (radius + Math.Sqrt(height * height + radius * radius)));
        }
        public double getVolumn()
        { return (Math.PI * radius * radius * height / 3); }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Sphere sp = new Sphere(2);
            Cylinder cd = new Cylinder(2, 10);
            Cone cn = new Cone(2, 20);
            ICalAreaAndVolumn[] array = { sp, cd, cn };

            Console.WriteLine("球的表面积为:" + array[0].getArea().ToString() + ";体积为:" + array[0].getVolumn().ToString());
            Console.WriteLine("圆柱的表面积为:" + array[1].getArea().ToString() + ";体积为:" + array[1].getVolumn().ToString());
            Console.WriteLine("圆锥的表面积为:" + array[2].getArea().ToString() + ";体积为:" + array[2].getVolumn().ToString());
            Console.ReadKey();
        }
    }
}


【注】接口的使用时的where & how,举例如下:


interface


分析


如何实现?


实现代码


main代码实现

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值