Java Training,Android training,IOS Training,. Net Training , look forward to communicating with you!
Inheritance, polymorphism, learning objectives:
1. Ability to memorize definitions and concepts
2, can understand its internal meaning
3, can apply to their own implementation of the Code
How to Reuse code:
-----wrote a piece of code, the ideal state (never write again, direct use)
-----When you write a class, and then write other classes, if they have a "common attribute" and "common method", we can consider the part that we share, extract it, convenient for subsequent reuse!
Steps:
1. Create a new data container (class)
2. Cut the properties and methods that need to be "reused" into the class (parent class)
3, after the new class, after its definition of the syntax, use: The way the parent class to establish the relationship between the two
----This is what we call "inheritance":
The purpose of inheritance: to implement "code reuse", the new class inherits the "original class", that is, the original class has the properties and methods.
Characteristics and characteristics of "inheritance":----Inherited class: Parent class, base class, inheriting class: Subclass, Derived class
1. After the subclass inherits the parent class, it has all the properties and methods of the parent class that are exposed externally
2. In the subclass method, how to use the parent class resource: base. Parent class resource, this. Parent resource (also your father)
3, but the parent cannot get the "subclass" of its own resources. (of a son or son)
4, not all "parent" resources can be used by the quilt class:
Private---Privately owned resources that can only be used in this class, and subclasses cannot be used.
Protected---Such resources are only used by the "family" object and cannot be accessed externally.
5. "Subclass" Inherits "parent class" satisfies: is a relation "subclass is a parent class"
----When our program, a class, can be completely replaced by another class! We'll just say that they meet the "Richter substitution principle" subclass can replace the parent class completely.
6, single-sex----A subclass can inherit only from one parent class
Transitive----The son can inherit the father, the grandson can inherit the son, and the more descendants of the class gets more resources
5. Descendants class when you create an object instance, it creates its parent class and then creates its subsequent subclasses.
6. Usage of Bass in sub-class:
1. In the subclass method, use base to invoke the properties and methods of the parent class.
2. After the subclass constructor, use: Base () to pass construction parameters to the parent class
Private |
In a class to be accessed |
Protected |
In a class, and in a descendant class, can be accessed |
Internal |
In this project (assembly) to be accessed |
Public |
Everyone has access to |
Tips:
To implement the reuse of an existing "class":
Way one: Inherit----> is a relationship-----> Zhang San is a John Doe (Zhang San said John Doe, with John Doe function)
Mode II: Employment----> has a relationship-----> Zhang San has a John Doe (Zhang San with a John Doe, with four function)
Comparison of the two methods:
Inheritance, not only can be used for its external properties and methods, but also can use its protected method, but the relationship is too close, not easy to expand and replace;
Hire, flexible, easy to expand, and more useful for multiplexing multiple resources at the same time
In-depth. NET (inheritance)