Analysis on the interfaces in C #

Source: Internet
Author: User

The concept of interfaces is not new at all. mainstream languages such as C ++, C #, and Java contain interfaces, which are very appealing.

It makes people love that it can help us complete some relatively complex operations and maintain logical relationships, especially when there are many complex types and inheritance relationships. It is annoying because it does not seem as easy to use as it is on the surface. It involves a lot of access control, coupled with inheritance and derivation between interfaces. Therefore, simply write an interface summary, and use the power to take Reading Notes, so the good memory is not as bad as the pen. 650) this. width = 650; "src ="/neweditor/editor/images/smiley/46.gif" alt = ""/>

First, let's do some homework on Knowledge reserves.

Subclass and base classes and their inheritance relationships. Code-free

 
 
  1. public class Animal 
  2.     public int NumberOfLegs; 
  3.     public void Eat() 
  4.     { 
  5.         //code to make the animal eat 
  6.     } 
  7.  
  8. public class Bird : Animal 
  9.     public double Wingspan; 
  10.     public void Fly() 
  11.     { 
  12.         // code to make the bird fly 
  13.     } 
  14.  
  15. public button1_Click(Object sender, EventArgs e) 
  16.     Bird tweety = new Bird(); 
  17.     tweety.Wingspan = 7.5; 
  18.     tweety.Fly(); 
  19.     tweety.NumberOfLegs = 2; 
  20.     tweety.Eat(); 

Class inheritance is implemented by the colon, followed by the base class name. Note that C # does not allow many inheritance, that is, a subclass can only inherit from one parent class. This is just like you can only have one father. The subclass inherits the parent class and automatically obtains all the public and protect methods and fields in the parent class. The private modifier methods and fields in the parent class are invisible to the Child class. In the parent class, protect is visible to the subclass and invisible to other classes. When a class is modified by sealed), it indicates that it cannot be inherited by other classes. I think it is very appropriate to use eunuch to describe it.

Subclass inheritance for the parent class is not only the acquisition of methods and fields, but also the most important thing is to ensure a downward extension of a logical relationship. Multiple child classes have one parent class, they all automatically inherit some attributes of the parent class. At the same time, there are also differences between them, just like the brothers who only look like, but they certainly have their own characteristics. Of course, we do not rule out the special circumstances of twins, but it does not seem to make any sense in programming ). Well, how can we ensure the difference between siblings? This relies on child classes to overwrite or replace parent classes.

Methods Modified by virtual keywords in the parent class can be overwritten in the subclass. Although the method names are the same, each subclass has different functions. The override must be used in the subclass to modify the method to be replaced.

 

 
 
  1. public class Bird 
  2.     public virtual void Fly() 
  3.     { 
  4.         // code to make the bird fly 
  5.     } 
  6.  
  7. public class Penguin : Bird 
  8.     public override void Fly() 
  9.     { 
  10.         MessageBox.Show("Penguins can't fly!"); 
  11.     } 

Birds all share a common method of Fly (), but not all birds can Fly, such as penguins and ostrich. Rewriting can solve this problem well. It not only ensures the class inheritance relationship between birds and penguins, but also solves the difference between different birds.

The following describes the access of the base class to the subclass. During program design, we can use the reference of the parent class to point to the subclass instance. Of course, this will have some disadvantages. Subclass is transparent to the parent class. The parent class does not know who inherits the class, but does not know what fields and methods are added to the subclass. Therefore, the reference of the parent class can only access fields and methods that already exist in the parent class. At first glance, it seems that the reference of a parent class to a subclass instance has no practical significance. However, this tips is often used in practical applications.

We can use an array referenced by the base class to save the subclass instance and access the common methods and fields in their parent class. The same method applies to interfaces. We will discuss it below.

Note that if the parent class has its own constructor, the Child class must also have constructor. If the parent class has multiple constructors, there must be at least one subclass. The constructor of the parent class is executed before the subclass construction. This is easy to understand.

The following describes the interfaces.

 

The first thing to note is that the interface is also a class, but it is more special than the class we usually use. The main reasons are as follows:

The interface can only be implemented by the class, and the class that implements this interface must implement all methods of the interface. An interface is like a contract to ensure that it has the features described in the interface.

The interface is more like a supplement to a class function. Every time a class implements this interface, it can be referenced to point to the instance of that class. Of course, similar to class inheritance, interface references can only access methods and attributes contained in interfaces. At the same time, a class can implement multiple interfaces. Of course, the responsibility is to implement methods in all interfaces. It's just like a person can have many godfathers.

Simply put, the inheritance of interfaces is not much different from that of classes. The reference of the base interface can point to the instance that implements the sub-interface class. Think about the practical significance of doing so. The interface can be considered as an additional function. Many different classes can have this function, and not all classes have inheritance relationships. However, they can all use an array referenced by an interface to access methods and properties in the interface ).

 

Parent-child relationships, including classes and interfaces, are converted from bottom to top. The following describes a special method to put this relationship upside down. The same as the parent can be used to operate the sub-methods.

The is and as keywords are required to implement this function.

 
 
  1. Public interface Isendmail
  2. {
  3. // An interface with the mail sending function attached
  4. Void sendmail ();
  5. }
  6. Public class Pigeon: Bird, Isendmail
  7. {
  8. //......
  9. Void sendmail ()
  10. {
  11. // Send mail...
  12. }
  13. }
  14. Bird [] bir = new Bird [2];
  15. Bir [0] = new Penguin ();
  16. Bir [1] = new pigeon ();
  17.  
  18. For (int I = 0; I <bir. Length; I ++)
  19. {
  20. If (bir [I] is Isendmail)
  21. {
  22. Isendmail mail;
  23. Mail = bri [I] as Isendmail;
  24. Mail. sendmail ();
  25. }
  26. }

Note that the interface reference can only access the methods and attributes declared in the interface.

At present, there are so many opportunities to summarize the virtual classes.

This article from the "xiangsen" blog, please be sure to keep this source http://xiangsen.blog.51cto.com/236167/622154

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.