抽象类实现求圆的面积

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

namespace 抽象类实现求圆的面积
{
    public abstract class myClass
    {
        private int r = 0;
        /// <summary>
        /// 圆半径
        /// </summary>
        public int R
        {
            get { return r; }
            set { r = value; }
        }
        /// <summary>
        /// 抽象方法,用来计算圆面积
        /// </summary>
        public abstract double Area();
    }
    public class DriveClass : myClass//继承抽象类
    {
        /// <summary>
        /// 重写抽象类中计算圆面积的方法
        /// </summary>
        public override double Area()
        {
            return Math.PI * R * R;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            DriveClass driveclass = new DriveClass();//实例化派生类
            myClass myclass = driveclass;//使用派生类对象实例化抽象类
            myclass.R = 3;//使用抽象类对象访问抽象类中的半径属性
            Console.WriteLine("圆面积为:" + myclass.Area());//使用抽象类对象调用派生类中的的方法
            Console.ReadKey();
        }
    }
}

有人评论说上面的代码是我复制粘贴过来的,确实是。我是因为这上面有注释适合初学者学习。也很好理解。这上面的代码和我写的也差不多,那好吧,有些人不希望需要这样的代码,我就把自己写的发出来吧,哈哈神奇
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _005
{
    public abstract class MyClass
    {
        private int myVar;

        public int MyProperty
        {
            get { return myVar; }
            set { myVar = value; }
        }
        public abstract double Area();
    }
    public class cx : MyClass
    {
        public override double Area()
        {
            return Math.PI * MyProperty * MyProperty;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {            
            //MyClass c = new cx();
            cx c = new cx();

            c.MyProperty = 10;
            double d = c.Area();
            Console.WriteLine(d);
            Console.ReadKey();
        }
    }
}

转载于:https://www.cnblogs.com/bb-love-dd/p/6007457.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值