C#--virtual,abstract,override,new,sealed

Source: Internet
Author: User

Virtual: Use this keyword to make it overridden in a derived class.

Abstract: An abstraction method that is overridden by a subclass, or continues to exist for an abstract method and is implemented by its child subclasses.

Override: Overrides an abstract implementation or virtual method of a parent class method, property, or event.

NEW: Explicitly hides the inherited members from the parent class.

Background code:

Public abstract class animal{public    abstract void Eat ();    Public virtual void sleep ()    {        HttpContext.Current.Response.Write ("Animal is sleeping! 
  
Foreground call Effect
    protected void Page_Load (object sender, EventArgs e)    {        Animal an1 = new Horse ();        An1. Eat ();        An1. Sleep ();        Animal an2 = new Cat ();        An2. Eat ();        An2. Sleep ();        Horse an3 = new Horse ();        An3. Eat ();        An3. Sleep ();        Cat An4 = new Cat ();        An4. Eat ();        An4. Sleep ();    }

Add:

When sealed modifies a method, the sealed must be used with override.

Sealed will enable you to allow classes to inherit from your class and prevent them from overriding specific virtual methods or virtual properties

public class cat:animal{public    sealed override void Eat ()    {        HttpContext.Current.Response.Write ("Cat is eating!") <br/> ");    }    Public new void Sleep ()    {        HttpContext.Current.Response.Write ("Cat is lying on sleeping! 
   

At this point, in the Litcat class, the override eat method is not present.

C#--virtual,abstract,override,new,sealed

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.