From :( http://hi.baidu.com/21tian/blog/item/aabfae6ed4a675de81cb4ab4.html)
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.
(4) use the new modifier to hide the base class members
The new modifier can be used to explicitly hide the members inherited from the base class. To hide an inherited Member, declare the member in the derived class with the same name and modify it with the new modifier.
See the following classes:
Public class mybase
{
Public int X;
Public void myvoke ();
}
Declaring a member with the myvoke name in a derived class hides the myvoke method in the base class, namely:
Public class myderived: mybase
{New Public void myvoke ();}
However, because field X is not hidden by similar names, this field is not affected.
Hiding names by inheritance takes one of the following forms:
A. The constant, specified, attribute, or type in the introduced class or structure hides all base class members with the same name.
B. The methods in the introduced class or structure hide the attributes, fields, and types of the base class with the same name. It also hides all base class methods with the same signature.
C. The indexer in the introduced class or structure hides all base class indexers with the same name.
Note: It is incorrect to use both new and override on the same member. Both new and virtual can ensure a new dedicated point. A warning is issued when the new modifier is used in the declaration that the inherited members are not hidden.
Example 1: In this example, the base class mybasec and the derived class myderivedc use the same field name X, thus hiding the value of the inherited field. This example illustrates the use of the new modifier. It also explains how to use a fully qualified name to access hidden members of the base class.
Using system;
Public class mybase
{
Public static int x = 55;
Public static int y = 22;
}
Public class myderived: mybase
{
New public static int x = 100; // use new to hide the X of the base class
Public static void main ()
{
// Print X:
Console. writeline (X );
// Access X of the hidden base class:
Console. writeline (mybase. X );
// Print the hidden Y:
Console. writeline (y );
}
}
Output: 100 55 22
If the new modifier is removed, the program continues to compile and run, but you will receive the following warning:
The keyword new is required on ''myderivedc. X' because it hides inherited member ''mybasec. x ''.
If the nested type is hiding another type, as shown in the following example, you can also use the new modifier to modify this nested type.