The difference between abstract method and virtual method in C#

1. Abstract method: only defined in abstract class, method modifier cannot use private, virtual, static

The abstract method code is as follows:


        public abstract class People   //声明一个抽象类
        {
    
    
            public abstract void study();  //抽象方法只能定义在抽象类中。
        }

        public class Student : People   //继承抽象类
        {
    
    

            public override void study()     //重写抽象类的抽象方法
            {
    
    
                Console.WriteLine("好好学习,天天向上!");
            }
        }

        public class Program
        {
    
    

            static void Main(string[] args)
            {
    
    
                Student t = new Student();//实例化派生类

                People p = t;   //使用派生类对象实例化抽象类

                //以上两句等价于  People p = new Student();//使用派生类对象实例化抽象类;

                p.study(); //使用抽象类对象调用抽象类中的抽象方法study    

            }
        }

Insert picture description here
Summary:
1. Like methods can only be declared in abstract classes, using the keyword abstract
2. Abstract methods in abstract classes must be rewritten by subclasses.
Abstract methods have no method bodies, and subclasses must rewrite method bodies!!, so abstract methods It can be seen as a virtual method without a method body

Second, the virtual method: the method of using virtual modification:

Virtual methods can have method bodies. Specific examples are as follows:



    public class BaseClass         //创建一个基类
    {
    
    
        public virtual string GetName()    //使用virtual关键字创建父类中的虚方法
        {
    
    
            return "父类虚方法体";   
        }
    }

    public class SubClass : BaseClass    //子类继承父类
    {
    
    
        public override string GetName()   //子类重写父类虚方法
        {
    
    
            return "重写父类虚方法!";
        }
    }

The above example: the virtual method in the parent class is overridden by the derived class.

Note: The virtual modifier cannot be used simultaneously with the private, static, abstract, and override modifiers.

ps: The override modifier cannot be used with the new, static, and virtual modifiers at the same time, and the override method can only be used to override the virtual method in the base class.

Limitations of virtual functions:
1. Virtual functions are only applicable to class objects with inheritance relationships, so only member functions of the class can be described as virtual functions;
2. Static member functions, inline functions, and constructors cannot be virtual functions;
3. The destructor can be a virtual function.

Three, the difference between the two:
summary:

The abstract method is only the method name, there is no method body (that is, no method is implemented), and the subclass must override the parent class abstract method.
The virtual function is that the method has a method body, but the subclass can or may not cover it.

1. A virtual method has a method body, and an abstract method has no method body. An abstract method is a method that forces a derived class to override, otherwise the derived class will not be instantiated.
2. Abstract methods can only be declared in abstract classes, virtual methods are not.
3. Derived classes must rewrite abstract methods in abstract classes, and virtual methods are not necessary.

Guess you like

Origin blog.csdn.net/qq_37192571/article/details/109066717