Virtual, override in C #

Source: Internet
Author: User
Tags abstract

C # is an object-oriented programming language in which each function belongs to a class.

When a method is declared static, this method is a static method, and the compiler retains the implementation of the method at compile time. That is, the method belongs to a class, but does not belong to any member, regardless of whether an instance of the class exists or not. Just like the entry function, static void Main, because it is a static function, it can be invoked directly.

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.

For example, there is a class Test,test class that has two methods Hello () and greet ():

public class test
{
public void Hello()
{
System.Console.WriteLine("hello, world!");
}
.....
}
Class Testme derived from class test, then when you use the following code:

Test a = new Testme ();

After creating a new instance of the Testme class, if you try to execute the following code:

A.hello ();

Then, naturally, the Hello () method of the base class test is run, but if you want to give the derived class Testme one of its own Hello () methods, you will declare the Hello () method in the test class as virtual:

public virtual void Hello()
{
...
}
Then, in the derived class, the method of overriding the base class is represented by the override keyword:

public class TestMe : Test
{
...
public overrice void Hello()
{
System.Console.WriteLine("hello from TestMe class!!!");
}
....
}

Then call a. Hello (), there will be "Hello from Testme class!!!" Words, not "hello,world!." The method that describes the base class has been overwritten. This is the manifestation of polymorphism.

It is not difficult to see from the above that a static method is real, and a virtual method can be overridden by derived classes, which are conflicting, in fact, for a method, C # stipulates that only one of these qualifiers can be used:

Override virtual Static abstract sealed

The meaning of the Representative is:

overloaded functions, virtual functions, static functions, abstract functions, sealed functions (not derived)

In addition, the declaration of a method defined in C # is:

Visibility type return value method name (argument list) {method body}

For example

public static void Test (int a) {System.Console.WriteLine (a.tostring ());}

Original address: http://www.cnblogs.com/mygood/articles/mygood_dome_c.html

This is a public static function, the function named Test, no return value, there is an integer parameter A, the function is to output a value on the screen.

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.