The difference analysis of virtual and abstract in asp.net _ Practical skills

Source: Internet
Author: User

This article analyzes the difference between virtual and abstract in ASP.net, and shares it for everyone's reference. The specific analysis is as follows:

Virtual method (virtual method)

The virtual keyword is used to decorate a method in a base class. The use of virtual 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 a derived class. In a call to a derived class instance, the virtual method uses the method defined by the base class.
Scenario 2: The virtual method is defined in the base class, and the method is overridden in a derived class using override. In a call to a derived class instance, the virtual method uses the method of the derived override.
When a method is declared as virtual, it is a dummy method until you use ClassName variable = new ClassName (), which does not exist in the real memory space until you declare an instance of a class. This keyword is commonly used in the inheritance of classes to provide support for the polymorphism of the class method.

Ii. Abstract Method (abstract method)

The abstract keyword can only be decorated with methods in an abstract class, and there is no specific implementation. An implementation of an abstract method must be implemented in a derived class using the Override keyword.
Abstract method declarations are used, is a method that must be overridden by a derived class, which is used to be inherited; it can be seen as a virtual method without an implementation body; If the class contains abstract methods, then the class must be defined as an abstract class, whether or not there are other general methods, and the abstract class cannot have an entity.

Three, polymorphic

The implementation of polymorphism in C # is divided into two types, one is compile-time polymorphism, the other is Run-time polymorphism
|-compile-time polymorphism through the overload of multiple methods in a class to achieve polymorphism, the system at the time of compilation, according to the parameters of the transfer to determine the specific call to the method of which overload;
|-Run-time polymorphism is realized by virtual function (virtual function), abstract method, derived class to override virtual function or abstract method, thus realizing run-time polymorphism.

Iv. The use of abstract

Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Text;
Namespace ConsoleApplication3
{
Public abstract class book
{
Abstract method, without the body, the class where the abstract method must be an abstract class, and the derived class must implement the method
public abstract void introduce ();
}
public class Javabook:book
{
Implementation of abstract methods, must be implemented, attention! You must add the Override keyword
public override void Introduce ()
{
Console.WriteLine ("I ' m Java");
}
}

public class Test
{
Public test ()
{
Javabook Javabook = new Javabook ();
Javabook.introduce (); Introduce () will be invoked in Javabook
Book book = new Javabook ();
Book.      Introduce (); Introduce () will be invoked in Javabook
}
public static void Main ()
{
Test T = new Test ();
}
}
}



v. the use of virtual and the use of override

Copy Code code as follows:
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 have a method to override 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 (); Introduce () will be invoked in Javabook
Book.       Introduce (); Introduce () will be invoked in Javabook
Javabook.sayhi (); Sayhi () will be invoked in Javabook
Book.           Sayhi (); Will call Sayhi () in book
}
public static void Main ()
{
Test T = new Test ();
}
}
}



vi. The difference between virtual and abstract

(1) The method of virtual modification must have an implementation (even if only a pair of curly braces is added), and the method of abstract modification must not be implemented. If the method for virtual decoration is not implemented:
(2), virtual can be overridden by the quilt class, and abstract must be overridden by the quilt class, if you rewrite the virtual modified method, you must add override (this tells the compiler you want to rewrite the virtual method), and must have implementation, or compile error;
(3) If the class member is abstract decorated, the class must be added abstract, because only abstract classes can have abstract methods.
(4), cannot create an instance of an abstract class, can only be inherited cannot be instantiated, for example: BaseTest2 base2 = new BaseTest2 (); A compilation error occurs: An abstract class or interface cannot create an instance.

(5) In C # If you want to override a method in a subclass, you must add virtual to the parent class method before adding override to the subclass method, which avoids the programmer inadvertently rewriting the parent class method in the subclass.

(6) The abstract method must be overridden, and the virtual method must have an implementation, even if it is a method defined in the abstract class.

I hope this article will help you with the ASP.net program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.