C# Abstract,override ,virtual 浅谈

本文介绍抽象类和抽象方法的基本概念与使用方法,通过一个具体的龙类实例演示了如何定义抽象类及其派生类,并展示了如何重写抽象方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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();
            }


        }
    }
}






### C# 中 `abstract` 和 `virtual` 关键字的区别及使用场景 #### 定义与特性 - **Virtual 方法**:可以在任意类中定义,具有具体的实现。派生类可以选择重写该方法,也可以继续使用基类的实现[^1]。 - **Abstract 方法**:必须定义在抽象类中,本身没有任何具体实现,强制要求派生类提供自己的实现[^2]。 #### 实现细节对比 | 特性 | Virtual 方法 | Abstract 方法 | |--------------------|-----------------------------------------------|-----------------------------------------| | 是否需要实现 | 提供默认实现 | 不允许提供实现 | | 存放位置 | 普通类或抽象类 | 必须位于抽象类 | | 强制子类覆盖 | 非强制 | 强制 | #### 示例代码比较 以下展示了两种关键字的实际应用差异: ```csharp using System; public class BaseClass { // Virtual method with default implementation public virtual void Display() { Console.WriteLine("Base Class Method"); } // Abstract method must be implemented by derived classes public abstract void Process(); } public class DerivedClass : BaseClass { // Override the virtual method public override void Display() { Console.WriteLine("Derived Class Overridden Method"); } // Implement the abstract method public override void Process() { Console.WriteLine("Processing in Derived Class..."); } } class Program { static void Main(string[] args) { BaseClass obj = new DerivedClass(); obj.Display(); // Calls overridden version from DerivedClass. obj.Process(); // Calls required implementation of abstract method. Console.ReadLine(); } } ``` #### 使用场景分析 - 当设计一个通用功能时,如果某些行为可能不需要改变或者已经有合理默认逻辑,则应采用虚拟函数(`virtual`);这样既提供了灵活性又保留了一定程度上的标准化处理流程[^1]。 - 若某个操作完全依赖于特定类型的内部状态而无法给出有意义的基础版本,并且期望所有继承者都重新定义此动作的话,则应该运用抽象成员(`abstract`)[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值