In-depth inheritance-abstract classes and interfaces

Source: Internet
Author: User
I. Basic concepts I. Disk end Abstract class:Also called abstract base class: A general class marked with abstract keywords during definition. It can contain all the features of a general class, such as fields, attributes, and methods. It also contains a special method, abstract METHODS (these methods are basically the function titles without code execution, and classes derived from this class must provide the code for execution) and cannot be instantiated, it is mainly used in the definition and partial implementation of classes, so it is necessary to fully expand and implement functions in the extended classes.

Abstract method:Abstract methods are called when abstract keywords are added to methods in a class. abstract methods can be used only in abstract classes and interfaces.

For example:

Public abstract class person {// first define an abstract person's base class public int eye; Public int clothes; Public Person (int yj, int YF) {eye = YJ; clothes = YF;} public abstract string getmore (); // defines an abstract method} public class richgril: person {// The class public int shoes of the wealthy girl heirs; // shoes public richgril (int yj, int YF, int xz): Base (YJ, YF) {eye = YJ; clothes = YF; shoes = xz ;} public override string getmore () {return "this is a rich girl. He has" + eye + "big eyes, and" + clothes + "beautiful clothes, and "+ shoes +" pair of shoes ";}} public class poorgril: person {// class public int shoes of the poor girl's heirs; // shoes public int work; public poorgril (int yj, int YF, int xz, int GZ): Base (YJ, YF) {eye = YJ; clothes = YF; shoes = xz; Work = GZ ;} public override string getmore () {return "this is a poor girl. He has" + eye + "big eyes, and" + clothes + "beautiful clothes, and "+ shoes +", but she needs to do "+ work +" work ";}}

Interface:He is also a special abstract class, marked with the interface keyword. His definition does not have the class keyword. He can include methods and attributes and events, but methods can only be abstract methods, any class derived from this interface must provide the code for execution. no modifier can be added before any interface member.

The modifiers available for the interface include new, public, protected, internal, and private. However, only one modifier can be contained in the same declaration, and the New Keyword can only appear in the set port, indicates the member of the same name inherited by the write operation.

 

Similar to a class, an interface can be inherited and developed. But the difference is that class inheritance not only indicates inheritance but also implements inheritance, but interface inheritance only indicates inheritance, the derived class can inherit the method implementation of the base class, while the derived interface only inherits the method description of the parent interface, but does not inherit the implementation of the parent interface.

This is a simple interface Example:

Interface firstdemo // declare the first interface {int count (int I, Int J); // declare an attribute} public class C: firstdemo {public int count (int I, int J) {return I + J ;}}

Ii. FAQ 1

1. the inheritance of interfaces does not only inherit one interface. What should I do if the inherited interface has a parent interface?

In this demonstration, we mainly illustrate the problem: if an interface with a parent interface is inherited, the methods and attributes of all its parent interfaces must also be implemented.

Interface I _2_a // declare the first interface {int A {Get; set;} // declare an attribute} interface I _2_ B // declare the Second Interface {int count (int I, int J); // declare a method} interface I _2_c: I _2_a, I _2_ B {}// declare that the third interface inherits the first two interfaces. // nothing is declared. But it actually inherits the attributes and methods of the first two interfaces. Public class I _2_l: I _2_c // declare a class, which inherits the I _2_c interface {int A; Public int A {get {return a ;}set {A = value ;}} public int count (int I, Int J) {return I * j * ;}}

2. For external interface access, if a parameter or method with the same name appears, its parent interface must be displayed.

Public interface I _3_a {int count {Get; set;} Int J (Int J);} public interface I _3_ B {void count (int I); Double J (double J );} public interface I _3_c: I _3_a, I _3_ B {} public class I _3_l {public void sum (I _3_c THC) {THC. count (); // error, which has two meanings: THC. count = 1; // error, which has two meanings: THC. count (1); // error, with ambiguity (I _3_a) THC ). count = 1; // correct (I _3_ B) THC ). count (1); (I _3_a) THC ). J (1); (I _3_ B) THC ). J (1); THC. J (1, 1.0); THC. J (1 );}}

3. Access to members in multiple inheritance in the external class

Public interface I _4_a {string F (string a);} public interface I _4_ B: I _4_a {New String F (string a);} public interface I _4_c: I _4_a {string T ();} /// <summary> /// the following interface should be noted that it inherits B and C, but the two have inherited a // at the same time, their two interfaces adopt different policies when they are internally defined. What should we do with our access? /// </Summary> Public interface I _4_d: I _4_ B, I _4_c {}; public class I _4_l {Public String test (I _4_d THC) // The accepted parameter type is {THC. T (); // This is easy to understand. He calls the T method thc of the c interface directly. F ("B interface method"); // This is the F method of the B interface directly accessed. Although the interface also has an F method, it is new here, that is to say, the upstream access (I _4_a) THC) is intercepted ). F ("method of the ainterface"); // access the F method (I _4_c) THC) of the ainterface by specifying the method ). F ("method of the ainterface"); // because the f method of the ainterface does not exist in the C interface, it can still be accessed directly (I _4_d) THC ). F ("B interface method"); // Similarly, it is the F method used to access B interface. It is the same as the direct access to the second line }}

4. Three problems are described in this example. One is the display implementation interface.

The interface member displayed for implementation cannot be accessed from the class instance. because it cannot be accessed in a class instance, but it can be accessed inside the class. When the class user does not need to use this interface member

This feature is particularly useful.

In addition, this is particularly useful when multiple interfaces have members of the same name.

To display the call, you must inherit the interface

Public interface I _5_a {string name {Get; set;} double price {Get; set;} string x_more (); String More ();} public class I _5_l: I _5_a {private string name; private double price; Public I _5_l () {name = "123"; Price = 888.888 ;}# region I _5_a member string I _5_a.name // display the implementation attributes, the modifier {get {return name;} set {name = value ;}} public double price {get {return price;} is not required for the display declaration ;} set {price = value ;}} string I _5_a.x_more () // display implementation method {String More = Name + price. tostring (); return more;} Public String More () {String More = Name + price. tostring (); return more ;}# endregion} 5. Rewrite public class I _6_l_2: I _6_l_1, I _6_a {string I _6_a.geturl () {return "region ";} string I _6_a.getname () {return "dashboard blog" ;}}/// <summary> // This class also inherits the class I _6_l_1, in addition, the override keyword is used to re-write the method implementation in I _6_l_1 // so when calling the method, the result is naturally conceivable /// </Summary> public class I _6_l_3: I _6_l_1 {public override string geturl () {return "";} public override string getname () {return "helloworld ";}} /// <summary> /// this class is obviously odd in the implementation of the method. It uses the New Keyword /// not a rewrite problem, but more serious, basically, the implementation of the parent class is overwritten. // </Summary> public class I _6_l_4: I _6_l_1 {public new string geturl () {return "Hello everyone ";} public New String getname () {return "welcome to the advanced training ";}}

6. ing:

Public interface I _7_a {string g ();} public interface I _7_ B {string g () ;}// note that, both A and B have a member method public class I _7_l: I _7_a, I _7_ B {# Region Public String g () {return "implicitly implemented: Asp.net ";} # endregion # region I _7_ B member string I _7_ B .g () {return "explicitly implemented: Asp.net, hello" ;}# endregion}

There is also some inheritance issues related to virtual methods, override and new, because the usage of Virtual Methods and override is different from that of new, and a blog cannot be clearly described, so put it in a separate blog.

----- For the original code, refer to the day-hitting video.


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.