In-depth analysis of C # inheritance mechanism 4

xiaoxiao2021-03-06  55

2) Hidden base class members

Think about it, if all classes can be inherited, what consequences will be brought? The hierarchical system of the class will become very pang, the relationship between the big category is mixed, and it will become very difficult to understand and use the class. Sometimes, we don't want the classes you have written inherit. Other Time, some classes have no need to inherit. C # proposes a seal class concept to help developers to solve this problem.

The seal uses Sealed modifiers in the statement so that this class will be inherited by other classes. If you try to use a seal as a base class of other classes, C # will prompt an error. It is reasonable, and the sealing cannot be an abstract class because abstract always wants to be inherited.

Which occasions are used in sealing? The seal can prevent other programmers from inheriting the class inadvertently. Moreover, the sealing can function as an effective effect. In fact, there is no derived class in the sealing. If there is a virtual member function in the sealing instance, the member function can be converted to non-virtual, and the function modifier Virtual is no longer entry into effect.

Let us look at the example below:

BSTRACT CLASS A {Public Abstract Void f ();} Sealed Class B: A {Public Override Void F () {// F specific implementation code}}

If we try to write the following code C: B {} c # will point out this error, telling you that B is a seal, can't try to derive any classes from B. (3) Sealed method We already know that the use of seals can prevent inheritance of the class. C # also proposes the concept of SeaDMethod to prevent overloading of the method in the derived class of the method. The method can use Sealed modifiers, when we call this method is a sealing method. Each member method of the class can be used as a sealing method to seal, and the imaginary method of the base class must be overloaded, providing a specific implementation method. Therefore, in the method's statement, Sealed modifiers are always used simultaneously with Override modifiers. Please see the example code below:

Using system; class a {public virtual void f () {console.writeline ("afline);} public virtual void g () {console.writeline (" AG ");}} Class B: a {Sealed Override public void F () {Console.Writeline ("bf");} Override public void g () {console.writeline ("bg");}} Class C: b {override public void g () {console.writeline ("cg ");}}

The class B has been overloaded for two imaginary methods in the base class A, where the F method uses SeaD modifiers to become a sealing method. The g method is not a sealing method, so in the derived class C of B, the method G can be overloaded, but the method F cannot be overloaded. (4) Use the New modifier to hide the base class member Use the New modifier to explicitly hide members inherited from the base class. To hide a member of the inheritance, use the same name to declare the member in the derived class, and use new modifier to form it. Please see the following categories:

Public class mybase {public int x; public void myvoke ();

In the derived class, use myvoke name declaration members to hide the MyVoke method in the base class, namely:

Public class myderived: mybase {new public void myvoke ();} However, because field x is not hidden by similar name, it will not affect this field. By inheriting hidden names, one of the following forms is employed: a, the constant in the introduction, the specified, attribute, or type hide all the base classes with the same name. B, the method in which the class or structure is introduced hidden the properties, fields, and types of the same name in the base class. At the same time, all the basic class methods with the same signature are also hidden. C, the indexer in the introduction class or structure will hide all the base class indexes with the same name. Note: Use New and Override simultaneously on the same member. At the same time, New and Virtual can guarantee a new dedicated point. Use the New modifier using the New modifier in the statement that does not hide the inheritance. Example 1: In this example, the base class MyBasec and the derived class MyderiveDC use the same field name x to hide the value of inherited fields. This example illustrates the use of the New modifier. Also explain how to use a fully qualified name to access the hidden members of the base class.

Using system; public class mybase {public static int x = 55; public static int y = 22;} public class myderive: mybase {new public static int x = 100; // Using new hidden class Xpublic static void main () {// Print x: console.writeLine (x); // Access hidden base class x: console.writeline (mybase.writeLine (mybase.writeline (y);}}

Output: 100 55 22 If the new modifier is removed, the program will continue to compile and run, but you will receive the following warning: The keyword new is required on 'myderivedc.x' because it hides inherited mebasec.x '. If Nested types are hiding another type, as shown in the following example, or use the New modifier to modify this nested type.

转载请注明原文地址:https://www.9cbs.com/read-83906.html

New Post(0)