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

题目如上所示

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;

        }

    }

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • C#(C-Sharp)是Microsoft的新编程语言,被誉为“C/C++家族中第一种面向组件的语言”。然而,不管...
    浮生岁月阅读 9,175评论 0 7
  • 抽象类和类成员 通过在类定义前面放置关键字 abstract,可以将类声明为抽象类。例如: public abst...
    天lc呐阅读 3,401评论 0 0
  • zy17枚举结构体枚举enum 类型名 {枚举项}定义在类外面enum WeekDays{Sunday,Monda...
    假装我不帅阅读 3,652评论 0 0
  • 渐变的面目拼图要我怎么拼? 我是疲乏了还是投降了? 不是不允许自己坠落, 我没有滴水不进的保护膜。 就是害怕变得面...
    闷热当乘凉阅读 9,796评论 0 13
  • 感觉自己有点神经衰弱,总是觉得手机响了;屋外有人走过;每次妈妈不声不响的进房间突然跟我说话,我都会被吓得半死!一整...
    章鱼的拥抱阅读 6,618评论 4 5

友情链接更多精彩内容