C#编程练习——类的继承

2020-11-11  本文已影响0人  时然然呢
题目如上所示

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace CSharpMethod

{

    class Program

    {

        static void Main(string[] args)

        {

            Cylinder cylinder = new Cylinder();

            cylinder.High = 12;

            cylinder.Radius = 3;

            Console.WriteLine("地面半径为{0},高为{1},体积为{2}。", cylinder.Radius, cylinder.High, cylinder.Volume());

        }

    }

    public class Circle

    {

        private double radius;

        public double Radius

        {

            set { radius = value >= 0 ? value : 0; }

            get { return radius; }

        }

        public double Area()

        {

            return Math.PI * radius * radius;

        }

    }

    public class Cylinder : Circle

    {

        private double high;

        public double High

        {

            set { high = value >= 0 ? value : 0; }

            get { return high; }

        }

        public double Volume()

        {

            return Area() * high;

        }

    }

}

上一篇下一篇

猜你喜欢

热点阅读