Define class members (methods) in C #. net)

Source: Internet
Author: User
/*
* Created by sharpdevelop.
* User: noo
* Date: 2009-8-16
* Time:
*
* Define a class member (method). All members have their own access level, public, private (default), internal (internal project), and protected (class, derived class)
* Public keyword static (static member)
* There is also the proprietary keyword virtual (virtual method, which can be rewritten in sub-classes). Abstract (abstract method, which can only be written in abstract classes,
* It must also be rewritten in a non-Abstract derived class). Override (override a base class method in the subclass. If override is used, you can also use sealed to specify it in the derived class.
* This method cannot be further modified) and extern (the method definition is placed elsewhere)
*
* For an abstract class, see abstractclass. CS.
*
*/
Using system;
Class methoda // common class
{
Public void Method1 () // common method
{
Console. writeline ("this is a common method in the base class ");
}
Public static void method2 ()
{
Console. writeline ("this is a static method in the base class ");
}
Public Virtual void method3 () // virtual Method
{
Console. writeline ("This is the first virtual method in the base class ");
}
Public Virtual void method4 () // virtual Method
{
Console. writeline ("this is the second virtual method in the base class ");
}
}
Class methodb: methoda
{
Public override void method3 ()
{
Console. writeline ("first execute the methods in the base class ");
Base. method3 (); // call the base class method base keyword
Console. writeline ("this is a method to override the first virtual method of the base class in the subclass ");
}
Public override sealed void method4 () // specify that this method cannot be further modified in the derived class
{
Console. writeline ("this is a method to override the second virtual method of the base class in the subclass ");
}
Public void mymethod ()
{
Console. writeline ("this is the sub-class method ");
}
}
Class Test
{
Static void main ()
{

Methoda A = new methoda ();
A. method4 (); // call the method in methoda

Methoda AA = new methodb ();
AA. method4 (); // call the method in mehtodb
// Neither a nor aa can call the mymethod method.

Methodb B = new methodb ();
B. method4 (); // call the method in mehtodb
B. mymethod (); // call the mymethod method
}
}
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.