1. abstract methods can only be declared in abstract classes. Virtual methods are not.
Abstract methods must be rewritten in a derived class, but virtual methods do not.
2. Abstract METHODS cannot declare method entities,
Abstract Public void SD ();
You can use the virtual method.
Public Virtual void SDF ()
{
Console. writeline ("");
}
3. The virtual method can realize polymorphism, but the abstract method cannot...
Interface abstract and virtual in C #
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, can overwrite
{
console. writeline ("this is a test: Virtual getname ()");
}< br> Public Virtual void getcontent () // virtual method, can overwrite
{
console. writeline ("this is a test: Virtual getcontent ()");
}< br> Public void getdate () // general method. If it is rewritten in a derived class, you must use the new keyword
{< br> console. writeline ("this is a test: void getdate ()");
}< BR >}
public class javatek: book
{< br> Public override void getprice () // The abstract method must be implemented.
{< br> console. writeline ("this is a test: javatek override abstract getprice ()");
}< br> Public override void getname () // overwrite the original method, not Required
{< br> console. writeline ("this is a test: javatek override virtual getname ()");
}< BR >}
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