asp. Analysis of the difference between virtual and abstract in net

  • 2021-01-25 07:28:08
  • OfStack

This paper analyzes the differences between virtual and abstract in asp.net, and shares it with you for your reference. The specific analysis is as follows:

1. Virtual method (virtual method)

The virtual keyword is used to decorate methods in the base class. ES11en can be used in two ways:
Scenario 1: The virtual method is defined in the base class, but the virtual method is not overridden in the derived class. Then in a call to an instance of a derived class, the virtual method uses the method defined by the base class.
Scenario 2: An virtual method is defined in a base class, and then overridden using override in a derived class. Then in the call to the derived class instance, the virtual method uses the derived overridden method.
When a method is declared as Virtual, it is a virtual method, until you use ClassName variable = new ClassName(); An instance of a class does not exist in the real memory space until it is declared. This keyword is commonly used in class inheritance to provide support for polymorphism in class methods.

2. Abstract method (abstract method)

The abstract keyword can only be used to modify methods in abstract classes and has no concrete implementation. The implementation of the abstract method must be implemented using the override keyword in the derived class.
An abstract method is a method that must be overridden by a derived class. An abstract class is intended to be inherited. It can be thought of as a virtual method with no implementation body; If a class contains abstract methods, then the class must be defined as abstract, regardless of whether it contains other generic methods. An abstract class cannot have an entity.

3. The polymorphism

There are two kinds of implementation of polymorphism in C#, one is compile-time polymorphism, and the other is run-time polymorphism
| - compile-time polymorphism by multiple methods in a class of overloading to achieve polymorphism, system at compile time, according to the specific call parameters passed to determine which overloaded methods;
| - runtime polymorphism by virtual function (virtual functions), abstract methods of polymorphism, derived classes to override virtual functions or abstract method, so as to realize the runtime polymorphism.

4. How to use abstract

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication3
{
    public abstract class Book
    {
        // An abstract method, without a body. The class in which the abstract method resides must be an abstract class. A derived class must implement the method
        public abstract void Introduce();
    }
    public class JavaBook : Book
    {
        // To implement an abstract method, you must implement it, note ! You must add override The keyword
        public override void Introduce()
        {
            Console.WriteLine("I'm Java");
        }
    }     public class test
    {
        public test()
        {
            JavaBook javaBook = new JavaBook();
            javaBook.Introduce();      // Will call JavaBook In the Introduce()
            Book book = new JavaBook();
            book.Introduce();      // Will call JavaBook In the Introduce()
        }
        public static void Main()
        {
            test t = new test();
        }
    }
}


5. virtual and override

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
{
    public abstract class Book
    {
        public virtual void Introduce()
        {
            Console.WriteLine("I'm book");
        }
        public virtual void SayHi()
        {
            Console.WriteLine("Hi, I'm book");
        }
    }     public class JavaBook : Book
    {
        public override void Introduce()
        {
            Console.WriteLine("I'm Java");
        }
        // Note that this method does not override The method of the parent class
        public void SayHi()
        {
            Console.WriteLine("Hi, I'm Java");
        }
    }     public class test
    {
        public test()
        {
            JavaBook javaBook = new JavaBook();
            Book book = new JavaBook();
            javaBook.Introduce();       // Will call JavaBook In the Introduce()
            book.Introduce();       // Will call JavaBook In the Introduce()
            javaBook.SayHi();      // Will call JavaBook In the SayHi()
            book.SayHi();           // Will call Book In the SayHi()
        }
        public static void Main()
        {
            test t = new test();
        }
    }
}


6. Differences between virtual and abstract

(1), virtual modification method must be implemented (even if it is only to add 1 pair of curly braces), and abstract modification method 1 must not be implemented. For example, if the virtual modified method is not implemented:
virtual can be overridden by a subclass, but abstract must be overridden by a subclass. If you override a method modified by virtual, you must add override (this tells the compiler that you need to override virtual methods).
If a class member is modified by abstract, then the class must be preceded by abstract, because only abstract classes can have abstract methods.
BaseTest2 base2 = new BaseTest2(); BaseTest2 base2 = new BaseTest2(); A compilation error will occur: The abstract class or interface cannot create an instance.

If you want to override a method in a subclass, you must add virtual before the method in the parent class and override before the method in the subclass. In this way, you can avoid the programmer accidentally overwriting the method in the subclass.

(6) The ES83en method must be overridden and the ES84en method must have an implementation (even if it is a method defined in the ES85en class).

Hope this article described to everybody asp.net program design is helpful.


Related articles: