Interface abstract and virtual in c #

Source: Internet
Author: User

 

Interface is used to declare an interface
1. Only some method conventions are provided, and method subjects are not provided.
For example
Public interface IPerson
{
Void getName (); // does not contain the method subject
}
2. methods cannot be modified using public abstract, without field variables or constructor.
3. The method can contain parameters.
For example
Public interface IPerson
{
Void getAge (string s );
}

Example 1 ):
Public interface IPerson
{
IPerson (); // Error
String name; // Error
Public void getIDcard (); // Error

Void getName (); // right
Void getAge (string s); // right
}

Interface implementation class
1. Consistent with the format of the inherited class, such as public class Chinese: IPerson {}
2. Each method in the interface must be implemented.

Example 2: inheritance Example 1
Public class Chinese: IPerson
{
Public Chinese () {}// add structure
Public void getName () {}// implement getName ()
Public void getAge (string s) {}// implement getAge ()
}

Abstract declaring abstract classes and abstract methods
1. The class of the abstract method must be an abstract class.
2. abstract classes cannot be directly instantiated and must be implemented by their derived classes.
3. the abstract method does not contain the method subject and must be implemented in override mode by the derived class. This is similar to the method in interface.

For example
Public abstract class Book
{
Public Book ()
{
}

Public abstract void getPrice (); // abstract method, excluding the subject
Public virtual void getName () // virtual method, which can overwrite
{
Console. WriteLine ("this is a test: virtual getName ()");
}
Public virtual void getContent () // virtual method, which can overwrite
{
Console. WriteLine ("this is a test: virtual getContent ()");
}
Public void getDate () // general method. If it is rewritten in a derived class, the new keyword must be used.
{
Console. WriteLine ("this is a test: void getDate ()");
}
}

Public class javatek: Book
{
Public override void getPrice () // implements the abstract method, which must be implemented
{
Console. WriteLine ("this is a test: javatek override abstract getPrice ()");
}
Public override void getName () // overwrite the original method, not required
{
Console. WriteLine ("this is a test: javatek override virtual getName ()");
}
}

The test is as follows:
Public class test
{
Public test ()
{
Javatek jbook = new javatek ();
Jbook. getPrice (); // call getPrice () in javatek ()
Jbook. getName (); // call getName () in javatek ()
Jbook. getContent (); // call getContent () in the Book ()
Jbook. getDate (); // call getDate () in Book ()

}
Public static void Main ()
{Test t = new test ();
}
}

The virtual flag method is a virtual method.
1. override this method in a derived class
2. Objects can also be called without Overwriting
3. The method without this mark (or any other mark) needs to be hidden from the original method by new when rewriting.

Abstract and virtual: override keywords are used for method rewriting.
Both methods and abstract methods in the interface require implementation.
 


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.