I understand interface abstract virtual override and new in C #.

Source: Internet
Author: User
I understand interface abstract virtual override and new2008-01-03 in C #

Interface is used to declare an Interface

1. Only the rule constraints of the method are provided, and the subject of the method is not provided.

Example:

Public interface iperson

{

Void getname (); // does not contain the Subject Method

}

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 (); // correct

Void getage (string S); // correct

}

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 Constructor

Public void getname () {}// implement getname ()

Public void getage () {}// implement getage ()

}

 

Abstract is used to declare 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.

 

Overrid and new

// Override cannot override non-virtual or static methods.
// The base method to be rewritten must be virtual, abstract, or rewritten.
// Therefore, virtual members are paired with override members,
// Although new can also be used to "hide" virtual members

// The following example shows the difference between rewriting and hiding to further understand the role of virtual members.
// Rewrite part ----------
Public class mybaseclass
{
Public Virtual void dosomething ()
{
Console. writeline ("base imp ");
}
}

Public class myderivedclass: mybaseclass
{
Public override void dosomething ()
(
Console. writeline ("derived imp ");
}
}

// The override method replaces the Execution Code in the base class, so that the following code uses the replacement code,
// Even if this is performed through the base class, this is also the case:

Myderivedclass myobj = new myderivedclass ();
Mybaseclass mybaseobj;
Mybaseobj = myobj;
Mybaseobj. dosomething ();

// The result is as follows:

Derived imp

// In addition, you can use the following code to hide the base class method:
Public class mybaseclass
{
Public Virtual void dosomething ()
{
Console. writeline ("base imp ");
}
}

Public class myderivedclass: mybaseclass
{
New public void dosomething ()
{
Console. writeline ("derived imp ");
}
}
// The base class method does not have to be virtual, but the result is the same. The above Code only needs to modify the line.
// For the virtual and non-Virtual Methods of the base class, the result is as follows:
Base imp
// Although the Execution Code of the base class is hidden, it can still be accessed through the base class.

 

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.