Inheritance is a very important feature in object-oriented programming, and it is another important feature - the basis of polymorphism.
4.1 Introduction of the concept of inheritance
Things in real life belong to a certain category. For example, a lion is a (is_a) animal. In order to count
The object-oriented language introduces the characteristics of inheritance (inherit) in the computer.
shown, using the class animal to represent the animal, use the class lion to represent the lion, with a hollow triangular arrow table
The inheritance relationship is shown.
Of the two classes that make up the inheritance relationship, animal is called the parent class (or base Class), and Lion
Called a subclass (child class).
Tips:
in some books, the parent class is called the superclass (
" inherit " " Span style= "FONT-FAMILY:CALIBRI;" > derives " relationship, Span style= "Font-family:uitpac+stkaiti;" > "B  inherit from a " a "
Conversely,"A derives from B".
There are two basic characteristics between the parent class and the child class:
- is a (is-a) relationship: A subclass is a special case of a parent class.
- Extended (Extends) Relationship: Subclasses have functionality that the parent class does not have.
The following C # code implements the inheritance relationship between the lion and animal classes:
As you can see, C # separates the parent class from the child class with a colon interval.
Access rights for Class 4.2 members
One of the major features of object-oriented programming is the ability to control the accessibility of class members. The current mainstream object-oriented languages are
Has the following three basic accessibility (table 2):
Table 2 access rights for class members
(1) Public and private
Public and private are primarily used to define member access permissions for a single class.
Take a look at the following sample code:
public class A
{
public int Publici;
private int Privatei;
protected int Protectedi;
}
When an object of a is created by the outside world, only the public instance field of a is accessible publici:
A = new A ();
A.publici = 100; Ok!
The private instance field of Class A privatei can only be used by its own instance method:
public class A
{
public int Publici;
private int Privatei;
protected int Protectedi;
private void F ()
{
Privatei = 100; Ok!
}
}
In the code above, the Private method F () of Class A accesses the private field Privatei. Note that as long as Class A is directly defined
instance method, regardless of whether it is public or private, can access the private instance field of the class itself.
(2) protected
Between the two classes that form an inheritance relationship, you can define an extended permission --protected.
When a class member is defined as protected, none of the outside classes can access it, but its subclasses can visit
Ask.
The following code details what parts of the parent class the subclass can access (example Project Inherits):
Class Parent
{
public int publicfield=0;
private int privatefiled=0;
protected int protectedfield=0;
protected void Protectedfunc ()
{ }
}
Class Son:parent
{
public void Childfunc ()
{
Publicfield = 100;//Correct! Subclasses can access the public fields of the parent class
privatefiled = 200;//Error! Subclasses cannot access private fields of the parent class
Protectedfield = 300; //correct! Subclasses can access the protection fields of the parent class
Protectedfunc (); //correct! Subclasses can access the protection method of the parent class
}
}
When a subclass object is created, the outside world can access the public members of the subclass and the public members of the parent class, as follows:
Thus, all public members of its parent class can be accessed through the subclass object, in fact, the outside world is not clear
Which public members of the object are from the parent class and which public members are from the subclass itself.
Summarize the access rights of the class members under the inheritance condition:
- Everything that doesn't have to be known to outsiders is private.
- All services that need to be delivered to the outside are public.
- All the " ancestral tricks "," secret " is protected.
(3) Internal
There is also an accessibility in C #, which is the " internal " accessibility determined by the keyword internal .
Internal is a bit like public, the outside class can also directly access the members of the class or class declared as internal, but this only
Confined within the same assembly.
The reader can simply interpret the assembly as a standalone DLL or EXE file. A DLL or EXE file
You can have more than one class in a class that can be accessed by a class in the same assembly, but a class in another assembly cannot access it.
This class is said to have internal access.
For example, a class library project ClassLibrary1 can generate a stand-alone assembly (assuming that the project is built after compilation
ClassLibrary1.DLL), which defines two classes A and B:
namespace ClassLibrary1
{
Internal class A
{
Internal int internali=0;
}
public class B
{
Public void F ()
{
a = new a(); //ok!
A. internali= 100; //ok!
}
}
}
Because class B belongs to the same assembly as class A, the code in B can create an object of a and access a
The declaration is a member of the internal Internali.
Outside of assembly ClassLibrary1.DLL, only objects of Class B declared as public can be created, not
Object of Class A that is declared as internal.
Internal is the default accessibility of C #, which means that if a class does not have any accessibility keywords before it
, it is internal .
For example, the above Class A can also be written as:
It's exactly the same as
Note, however, that in a class, if you omit the Accessibility keyword for a member, the default is private.
For example:
Equivalent
For the convenience of readers, summarize the access permissions for C # 2.0 and Visual Basic 2005 as shown in Table 3.
Table 3 List of type access rights
4.3 The reciprocal assignment of a subclass's parent class variable
There is an important attribute between the parent and child class objects that make up the inheritance relationship:
Sub-class objects can be used as base class objects.
This is because the subclass object is a (is_a) parent object, so the following code is legal:
Parent p;
Son C = new Son ();
p = C; Correctly, the subclass object can be passed to the parent class variable
In the preceding code, the parent is the father of the son class.
However, the reverse is not possible and the following code is wrong:
c = P; Error, Parent object variable cannot be directly assigned to a subclass variable
If you are sure that the object referenced in the parent class variable is indeed a subclass type, you can use the type cast to assign
Value, whose syntax format is:
Sub-class object variable = ( subclass name ) base class object variable ;
or using the AS operator
Sub-class object variable = base class object variable as subclass name ;
The sample code is as follows:
c = (child) p; Correct, the parent object variable refers to the subclass object
Or
c = p as child;
Inheritance (UP)