C# Abstract,override ,virtual 浅谈


abstract 

修饰符指示所修饰的内容缺少实现或未完全实现。 

修饰符可用于类、方法、属性、索引器和事件。 

在类声明中使用以指示某个类只能是其他类的基类。 

标记为抽象或包含在抽象类中的成员必须通过从抽象类派生的类来实现。

 

抽象类不能实例化。

·            抽象类可以包含抽象方法和抽象访问器。

·            不能用 sealed修饰符修饰抽象类。 

·            从抽象类派生的非抽象类必须包括继承的所有抽象方法和抽象访问器的实际实现。

override 

扩展或修改继承的方法、属性、索引器或事件的抽象实现或虚实现

 

virtual 

关键字用于修饰方法、属性、索引器或事件声明,并使它们可以在派生类中被重写。




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


namespace Abstract
{
    abstract class 龙
    {
        protected int _风 = 20;


        protected int _火 = 20;


        protected int _雷 = 20;


        protected int _电 = 20;


        protected int _雨 = 20;


        public abstract void StrengThen();


        public virtual void Attack()
        {
            StrengThen();


            System.Console.WriteLine("风=" + this._风);


            System.Console.WriteLine("火=" + this._火);


            System.Console.WriteLine("雷=" + this._雷);


            System.Console.WriteLine("电=" + this._电);


            System.Console.WriteLine("雨=" + this._雨);
        }
    }
}


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


namespace Abstract
{
    class 风 : 龙
    {
        public override void StrengThen()
        {
            this._雨 = 0;
            this._火 = 0;
            this._雷 = 0;
            this._电 = 0;
            this._风 = 100;
        }
    }
}


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


namespace Abstract
{
    class 火 : 龙
    {
        public override void StrengThen()
        {
            this._雨 = 0;
            this._火 = 100;
            this._雷 = 0;
            this._电 = 0;
            this._风 = 0;
        }
    }
}


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


namespace Abstract
{
    class 雷 : 龙
    {
        public override void StrengThen()
        {
            this._雨 = 0;
            this._火 = 0;
            this._雷 = 100;
            this._电 = 0;
            this._风 = 0;
        }
    }
}


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


namespace Abstract
{
    class 电 : 龙
    {
        public override void StrengThen()
        {
            this._雨 = 0;
            this._火 = 0;
            this._雷 = 0;
            this._电 = 100;
            this._风 = 0;
        }
    }
}


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


namespace Abstract
{
    class 雨 : 龙
    {
        public override void StrengThen()
        {
            this._雨 = 100;
            this._火 = 0;
            this._雷 = 0;
            this._电 = 0;
            this._风 = 0;
        }
    }
}


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


namespace Abstract
{
    class 保护 : 龙
    {
        public override void StrengThen()
        {
            this._雨 = 0;
            this._火 = 0;
            this._雷 = 0;
            this._电 = 0;
            this._风 = 0;
        }


        public override void Attack()
        {
            System.Console.WriteLine("风=" + this._风);


            System.Console.WriteLine("火=" + this._火);


            System.Console.WriteLine("雷=" + this._雷);


            System.Console.WriteLine("电=" + this._电);


            System.Console.WriteLine("雨=" + this._雨);
        }


    }
}


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


namespace Abstract
{
    class Program
    {
        static void Main(string[] args)
        {
            龙 龙 = null;


            string[] Attack = {"风","火","雷","电","雨","保护"};


            foreach (string Item in Attack)
            {
                switch (Item)
                {
                    case "风":
                        龙 = new 风();
                        break;
                    case "火":
                        龙 = new 火();
                        break;
                    case "雷":
                        龙 = new 雷();
                        break;
                    case "电":
                        龙 = new 电();
                        break;
                    case "雨":
                        龙 = new 雨();
                        break;
                    default:
                        龙 = new 保护();
                        break;
                }
                龙.Attack();
            }


        }
    }
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值