C#实践(六)--------类的学习与实践(二)

题目:
编写一个“圆”类,能动态确定圆的半径,并计算圆的周长和面积。再编写一个“圆柱体”类,继承于“圆”类,并计算圆柱体的表面积和体积。编写一个控制台测试程序
代码:

Circle:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test3
{
    class Circle
    {
        const double PI = Math.PI;
        private int center_x = 0;
        private int center_y = 0;
        private double radius;
        public double Radius
        {
            get { return radius; }
            set { radius = value; }
        }
        public Circle()
        { }
        public Circle(double radius)
        {
            this.radius = radius;
        }
        public double area()
        {
            double s = PI * this.radius * this.radius;
            return s;
        }
        public double perimeter()
        {
            double l = 2 * PI * this.radius;
            return l;
        }
    }
}

Cylinder:

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

namespace Test3
{
    class Cylinder: Circle
    {
        private double height;
        public double Height
        {
            get { return height; }
            set { height = value; }
        }
        public Cylinder()
        { }
        public Cylinder(double radius,double height) : base(radius)
        {
            this.height = height;
        }
        public new double area()
        {
            double s = 2 * base.area() + base.perimeter() * this.height;
            return s;
        }
        public double volume()
        {
            double v = base.area() * this.height;
            return v;
        }
    }
}

测试类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Test3
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                double radius;
                Console.WriteLine("输入圆的半径");
                radius = double.Parse(Console.ReadLine());
                Circle circle = new Circle(radius);
                double s = Math.Round(circle.area(), 2);
                double l = Math.Round(circle.perimeter(), 2);
                Console.WriteLine("圆的面积为:" + s.ToString());
                Console.WriteLine("圆的周长为:" + l.ToString());
                Console.WriteLine("-----------------------------");
                Console.WriteLine("输入圆柱的半径");
                radius = double.Parse(Console.ReadLine());
                Console.WriteLine("输入圆柱的高");
                double height = double.Parse(Console.ReadLine());
                Cylinder cylinder = new Cylinder(radius, height);
                s = Math.Round(cylinder.area(), 2);
                double v = Math.Round(cylinder.volume(), 2);
                Console.WriteLine("圆柱的表面积为:" + s.ToString());
                Console.WriteLine("圆柱的体积为:" + v.ToString());
                Console.WriteLine("-----------------------------");
            }
            catch
            {
                Console.WriteLine("您输入的有错误.");
            }
            Console.WriteLine("按回车继续!");
            Console.ReadLine();
        }
    }
}

结果:
C#实践(六)--------类的学习与实践(二)_第1张图片

总结:
主要是使用继承,这是面向对象的一个特点,通过重载使代码的重用性提高,封装度也有一定的提高;在以往的基础上,本实验中明白了几个关键字的使用,C#使通过base来调用基类的成员,在派生类中使用new关键字来明确表明隐藏基类的成员。当然在隐藏后仍可以通过base关键字来调用基类的成员。还有构造和析构函数的调用次序。

题目:
定义一个形状类Shape,利用它作为基类派生出Rectangle、Circle等具体形状类,已知具体形状类均具有两个方法Area和Perimeter,分别用来求形状的面积和周长。最后编写一个控制台测试程序对产生的类的功能进行验证。

代码:
抽象类:Shape

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

namespace Test3
{
    public abstract class Shape
    {
        public abstract double Area();
        public abstract double Perimeter();
    }
}

派生类:
Circle:

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

namespace Test3
{
    class Circle:Shape
    {
        private double radius;
        public double Radius
        {
            get { return radius; }
            set { radius = value; }
        }
        public Circle()
        { }
        public Circle(double radius)
        {
            this.radius = radius;
        }
        public override double Area()
        {
            return Math.Round(Math.PI * radius * radius, 2);

        }
        public override double Perimeter()
        {
            return Math.Round(2 * Math.PI * radius, 2);
        }
    }
}

Rectangle:

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

namespace Test3
{
    class Rectangle : Shape
    {
        private double length;
        private double width;

        public double Length
        {
            get { return Width; }
            set { length = value; }
        }

        public Rectangle()
        { }
        public Rectangle(double length,double width)
        {
            this.length = length;
            this.width = width;
        }

        public double Width
        {
            get { return width; }
            set { width = value; }
        }

        public override double Area()
        {
            return Math.Round(length * width);
        }

        public override double Perimeter()
        {
            return Math.Round(2 * (length + width));
        }
    }
}

控制台测试程序:

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

namespace Test3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("-------------------------");
            Console.WriteLine("Circle的测试");
            Console.WriteLine("-------------------------");
            Console.WriteLine("请输入圆的半径");
            double r = double.Parse(Console.ReadLine());
            Circle circle = new Circle(r);
            Console.WriteLine("圆的面积为:{0}",circle.Area());
            Console.WriteLine("圆的周长为:{0}",circle.Perimeter());
            Console.WriteLine("-------------------------");
            Console.WriteLine("Rectangle的测试");
            Console.WriteLine("-------------------------");
            Console.WriteLine("请输入矩形的长");
            double l = double.Parse(Console.ReadLine());
            Console.WriteLine("请输入矩形的宽");
            double w= double.Parse(Console.ReadLine());
            Rectangle rectangle = new Rectangle(l,w);
            Console.WriteLine("矩形的面积为:{0}",rectangle.Area());
            Console.WriteLine("矩形的周长为:{0}",rectangle.Perimeter());
            Console.WriteLine("-------------------------");
            Console.WriteLine("按回车结束.......");
            Console.ReadLine();
        }
    }
}

C#实践(六)--------类的学习与实践(二)_第2张图片
总结:
主要是对抽象类的使用(抽象类包含了其派生类共有的特性),我主要知道了以下几点:
抽象方法必须定义在抽象类中;
派生类必须实现抽象基类的抽象方法,有两种实现方法:一是:重载,二是:抽象+重载。
抽象方法中不用执行代码。

你可能感兴趣的:(C#,c#)