C # Complete Manual (05)

xiaoxiao2021-03-06  85

11.2 Members Access and Inheritance

Like Java, the C # derived class cannot access private members in the base class.

"Private members are still in private, any code other than class, including derived classes, no right to access it."

?

11.2.1 Using Protection Access

"Protection members are public in the hierarchy of the class, while hierarchy is private."

?

11.3 Construction functions and inheritance

Both type constructors and derived class constructors must be executed, the order of calls will begin by the first base class, until the last derived class.

Use Keywords: base. You can call the base class constructor, or you can access the base class member hidden by the derived class member.

?

11.4 Inheritance and Name Hide

The name of the derived class definition is the same as the name of the base class member, but at this time, in the derived class, the same member of the name in the base class is hidden, and a warning message is generated when compiling. If you have to hide the base class member, you must avoid warnings, you must add a New keyword in front of the derived class member.

The new NEW used here is completely different from New when creating an object instance.

?

11.8 virtual method and overload ?????

The virtual method is to declare a Virtual (virtual) in the base class, while the method of declaring in one or more derived classes. Therefore, each derived class can contain the virtual method of its own version.

In the base class, the keyword Virtual will declare a virtual method in front of the statement statement statement. When derived class redefines the virtual method, you need to use an Override modifier. The process of defining the virtual method in the derived class is called method overload. The virtual method cannot be specified as Static or Abstract.

Overloading methods are unnecessary. If the derived class does not provide the virtual method's own version, then the virtual method in the base class is used.

The attribute can also be modified with the Virtual keyword and overload it with Override.

There is no need to modify the keywords in Java, and when the method of the child (derived class) and the superclass (base class) method, the overload (overload) is performed.

11.8.1 Why is you going to overload?

The overload method allows C # to support the runtime polymorphism.

Only virtual methods support overload, judge the implementation of the specific method version at runtime.

?

11.9 Use abstract class

The abstract method defaults to virtual type without having to use Virtual modifiers.

Classes containing one or more abstract methods must also be declared as abstract, declarative ways to add Abstract statements in front of class declaration statements.

?

11.10 Use SeaD to prevent inheritance

In C #, the inheritance is prevented by using the keyword Sealed.

In Java, you should prevent the inheritance class, you have to set the default constructor of this class as private.

11.11.1 sealing and unpacking

When the value type is referenced by the Object reference, a "boxing" process is generated. The sealing is stored in the object instance.

Unboxing is a process of obtaining a value from an object.

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

New Post(0)