Zookeeper
This article mainly resolves the most important concept about object-oriented-interfaces.
For an Interface, C # requires that the Interface keyword be used to declare the Interface.Its declaration is consistent with that of the class. An interface is a special abstract class.. The following code:
Class Program {static void Main (string [] args) {}}// declare an interface that can fly IRunable {// contains the method void Run ();} that can be implemented by the inherited subclass ();}
It can be seen from the previous knowledge of abstract classes that abstract classes cannot be instantiated (because they contain abstract members, and abstract members do not contain method bodies ). Can an interface be instantiated? The answer is yes and cannot be instantiated. See the following code:
At this time, the compiler tells us that we cannot create an instance of an abstract class or interface.
2. The interface can define which Members
1) An interface is an abstract class with certain capabilities ",Since an interface is a class, which Members can be defined internally?
First, fields, attributes, methods, indexers, and abstract methods can be used in common classes. What about interfaces?
See the following field declaration. The compiler will report an error, telling us that the field cannot be declared in the interface.
Since there are no fields in the interface, there is no encapsulation field. Therefore, the code of the encapsulated field shown in the preceding figure is also incorrect.
For the same reason, the code above can also know that explicit attributes cannot be defined in the interface (because the operation field assignment is required in the attribute, but the field cannot be declared in the interface ).
Can an interface declare automatic attributes? See the following code:
// Declare an interface IRunable {// declare the field int nAge {get; set;} string strName {get; set ;} /// include the method void Run () implemented by the inherited subclass ();}
The code can be compiled smoothly. Why? This depends on the source code of. NET. The comparison result after I compile the source code is as follows:
Abstract methods do not need to be used much. The original interface is an abstract love class. Of course, abstract classes can be defined, but abstract keywords are not used, and methods must not have a method body;
2) subclass of the inherited interface must implement all abstract members of the interface.
Let's take a look at the following code:
// Declare an interface, which contains attributes and unimplemented void interface IRunable {string strName {get; set;} void Run ();}
The following is an implementation class:
Class Person: IRunable {public void Run () {Console. WriteLine ("I can Run! ");}}
At this time, what will the compiler tell us when we compile? For example:
Therefore, classes that inherit interfaces must implement all abstract members of interfaces.
The correct code is as follows:
Class Person: IRunable {public void Run () {Console. WriteLine ("I can Run! ") ;}Public string strName {get {return strName ;}set {strName = value ;}}}
The code above shows that:
① Our inheritance class does not need to use the override keyword when implementing interface members.
② When implementing interfaces, the signatures must be consistent
Do we have such questions as when to use abstract classes and when to use interfaces?
Summary:
① Use abstract class: you can find the parent class and want to inherit from the Child class to some Members.
② Use interface: the interface is purely implemented for standardization. For example, if multiple classes have the same method but cannot find the parent class, you can define the method in the interface. Implement these classes.
The following disputes do not refer to the code at both ends. the similarities and differences between abstract classes and interfaces are compared. The first is abstract classes:
Class Program {static void Main (string [] args) {Student s = new Student (); // The Student class obtains the NAge attribute s through inheritance. NAge = 10; s. eat (); Console. writeLine ("-------- Student and Worker classes respectively obtain non-Private Members of the parent class through inheritance, implementing the abstract method --------"); Worker w = new Worker (); // Worker class obtains the NAge attribute w through inheritance. NAge = 40; w. eat (); Console. readKey () ;}// defines the parent class abstract class Person {private int nAge; public int NAge {get {return nAge;} set {nAge = valu E ;}} private void Run () {Console. WriteLine ("I am a parent class, I can Run! ");} Public abstract void Eat ();} class Student: Person {// subclass overwrites the abstract method of the parent class public override void Eat () {Console. writeLine ("I Am a subclass, And I inherit the parent class. I can eat at school! ") ;}} Class Worker: Person {// Similarly, Worker also obtains the non-private member public override void Eat () {Console through inheritance. writeLine ("I Am a subclass, I inherit the parent class, I can eat at the factory ");}}
Next, let's take a look at how the interface standardizes the implementation of multiple classes.
Class Program {static void Main (string [] args) {Student s = new Student (); s. strName = "pupils"; s. run (); Console. writeLine (s. strName); Console. writeLine ("--------------------"); Worker w = new Worker (); w. strName = "I Can't blame"; w. run (); Console. writeLine (w. strName); Console. readKey () ;}} interface IRunable {// The standard subclass must implement the strName attribute string strName {get; set ;}// the standard subclass must implement Run () method void Run ();} class Stude Nt: IRunable {// The Field string strname; public string strName {get {return strname;} set {strname = value ;}} public void Run () {Console. writeLine ("I'm a primary school student. I am running in school! ") ;}} Class Worker: IRunable {string strname; public string strName {get {return" Worker ";}set {strname = value ;}} public void Run () {Console. writeLine ("I'm a worker, I need to run in the factory! ");}}
Can the code above find that an interface is only implementing a standard subclass, while an abstract class can be inherited to some members of the subclass through inheritance.
Finally, let's take a look at the interface display implementation. Let's first look at the Common Implementation of the interface (the above Code implements the interface by implicit implementation)
Interface IRunable {// The standard subclass must implement the strName attribute string strName {get; set;} // The standard subclass must implement the Run () method void Run ();} class Student: IRunable {// here is the subclass field string strname; public string strName {get {return strname;} set {strname = value ;}} public void Run () {Console. writeLine ("I'm a primary school student. I am running in school! ");}}
Explicit interface implementation
Class Student: IRunable {// here is the string strname field of the subclass; // display the implementation interface string IRunable. strName {get {return strname;} set {strname = value;} void IRunable. run () {Console. writeLine ("I'm a primary school student. I am running in school! ");}}
The displayed implementation interface is used to solve the problem of Method Name Conflict. However, the interface is displayed, and a problem occurs in the code above, such:
Why?
Because the methods for explicitly implementing interfaces are private, they cannot be called through object variables. So how should we call it? See the following code:
Class Program {static void Main (string [] args) {// class replacement principle. The parent class variable points to the subclass object, and call the subclass method IRunable ir = new Student (); ir. run (); Console. readKey () ;}} interface IRunable {// The standard subclass must implement the strName attribute string strName {get; set ;}// the standard subclass must implement Run () method void Run ();} class Student: IRunable {// here is the subclass field string strname; // display the implementation interface string IRunable. strName {get {return strname;} set {strname = value;} void IRunable. R Un () {Console. WriteLine ("I'm a primary school student. I am running in school! ");} // Student s = new Student ();}
The output is as follows:
An explicit interface. The method of this interface can only be called through interface variables.
The interface export diagram is summarized as follows:
Graduation internship exchange group: 221376964. You can also pay attention to my Sina Weibo.