Interface, abstract, and virtual [zz] 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.

I found that many of my friends are not very clear about the abstract and virtual functions in C #. I will use two pieces of code to show you the differences between the two ~~ Development Environment: vs.net 2005 usage: Use the following code to overwrite the code in program. CS, and then run the first code: Abstract usage using system;
Using system. Collections. Generic;
Using system. Text; namespace leleapplication3
{
Public abstract class Book
{
// Abstract method, excluding the subject. The class of the abstract method must be an abstract class. The derived class must implement this method.
Public abstract void introduce ();
} Public interface iBook
{
} Public class javatek: Book
{
// Implement the abstract method, which must be implemented. The override keyword must be added.
Public override void introduce ()
{
Console. writeline ("I'm Java ");
} /// Compile Error
// Public void introduce ()
//{
// Console. writeline ("I'm Java ");
//}
} Public class test
{
Public test ()
{
Javadek = new javadek ();
Javatek. Introduce (); // call introduce () in javatek ()
Book = new javatek ();
Book. Introduce (); // call introduce () in javatek ()
}
Public static void main ()
{
Test T = new test ();
}
}
} Second code: Virtual usage and override usage using system;
Using system. Collections. Generic;
Using system. Text; namespace leleapplication2
{
Public abstract class Book
{
Public Virtual void introduce ()
{
Console. writeline ("I'm book ");
} Public Virtual void sayhi ()
{
Console. writeline ("Hi, I'm book ");
}
} Public class javatek: Book
{
Public override void introduce ()
{
Console. writeline ("I'm Java ");
} // Note that this method does not have the override parent class Method
Public void sayhi ()
{
Console. writeline ("Hi, I'm Java ");
}
} Public class test
{
Public test ()
{
Javadek = new javadek ();
Book = new javatek ();
Javatek. Introduce (); // call introduce () in javatek ()
Book. Introduce (); // you will call introduce () javatek. sayhi () in javatek; // you will call sayhi () in javatek ()
Book. sayhi (); // call sayhi () in the book ()
} Public static void main ()
{
Test T = new test ();
}
}
} Code 3: using system for new;
Using system. Collections. Generic;
Using system. Text; namespace leleapplication4
{
Public abstract class Book
{
Public void introduce ()
{
Console. writeline ("I'm book ");
} Public void sayhi ()
{
Console. writeline ("Hi, I'm book ");
}
} Public class javatek: Book
{
// New is not added, but C # adds new to the default behavior
Public void introduce ()
{
Console. writeline ("I'm Java ");
} // Add new explicitly. The actual effect is the same as that without new, but after adding new, compile warning is eliminated.
Public new void sayhi ()
{
Console. writeline ("Hi, I'm Java ");
}
} Public class test
{
Public test ()
{
Javadek = new javadek ();
Book = new javatek (); javatek. Introduce (); // call introduce () in javatek ()
Javatek. sayhi (); // call sayhi () in javatek ()

Book. Introduce (); // call introduce () in the book ()
Book. sayhi (); // call sayhi () in the book ()
}
Public static void main ()
{
Test T = new test ();
}
}
}
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.