Inheritance of Classes
Simple to understand:
When a class a has some characteristics, another class B, whichalso has all of the characteristics of Class A, and may have some more of its own, then we can implement:Class B uses a Feature information and continue to add some of your own unique feature information.
Basic concepts
- Inheritance: A class obtains its attributes from another existing class, called inheritance.
- Derivation: Produces a new class from an existing class, called a derivation.
Inheritance and derivation, in fact, are only expressed in different directions (angles), which is essentially a matter.
- Parent class/Subclass: An existing class is a parent class, and a new class is a subclass. The parent class is also called the base class, and the subclass is also called a derived class.
- Single inheritance: A class can inherit its attribute information only from a parent class. PHP and most object-oriented languages are single-inheritance modes. C + + is multiple inheritance.
- Extensions: Define some of your own new and unique attribute information (properties, methods, and constants) in the subclass. Without extension, inheritance is meaningless.
Access (permission) modifier
Members in a class can usually be preceded by the following 3 modifiers:
public: Communal, shared, public
Protected: Protected
Private: Privately-owned,
Public Public-
Members decorated with this modifier can be Used (accessed) at any location.
Access (using) is such a syntax pattern:
Objects , and members;
Class name :: member;
The access location is divided into 3 x:
1: Inside a class: Naturally, in a method of that class
2: The Inner of a child (parent) class that has an inheritance relationship to a class: Refers to a method in another class.
3, outside of a class: it is generally a separate code area (not in a class), similar to our previous code.
The code demonstrates the following:
protected the protected
A protected decorated member that can be accessed in a class with an inheritance relationship in the current class or in the hierarchy of the current class.
Private of Private
A member of the private adornment, accessible only in the class in which it resides.
Summary of access modifier qualifiers:
Parent represents the parent class
Contrast: Self stands for " This class "(Own current Class)
The parent is typically used when calling members of the parents in a subclass, most often using a static class member of the parent class .
-- Because the parent represents the class, not the object.
The following are 2 common uses of the parent keyword:
The representation of construction method in inheritance
- When a constructor method is not defined in a subclass, the constructor method of the parent class is automatically called. Therefore, when you instantiate a subclass, you do so in the form of the parent class's construction method.
Modified to:
- When a subclass defines its own constructor method, the constructor of the parent class is not automatically called, but it can be called manually: Parent::__construct ();
But usually, in subclasses, many times, in a constructor method, you should (need) call the constructor of the parent class to save code and increase readability:
- The destructor method of the parent class is automatically called when no destructor is defined in the subclass.
- When a subclass defines its own destructor, the destructor of the parent class is not automatically called, but it can be called manually: Parent::__destruct ()
rewrite Override
Rewriting, or overwrite, is to redefine the property or method inherited from the parent class --that is, from the new write.
Note: The subclass overrides the parent class method, although it is possible to call the parent class with the same name method to do some work, but it is not necessary. It is also possible that the result of the method performed by the parent class is not suitable for subclasses, at which time the subclass is completely self-written.
Basic requirements for rewriting:
Access control permissions:
Subordinate access control permissions should not be lower than the superior access control permissions:
Superior:Public subordinate: only Public
Superior:protected Subordinate: protected, Public
Superior:Private subordinate:private protected public-- The actual situation is meaningless.
Private can not be covered, but completely as a brand-new.
The Parameter form of the method:
Should be consistent with the parent class.
The right approach:
More parameters are also incorrect:
- Overriding problem with private properties and private methods: private properties and methods cannot be overridden, but subclasses can define properties or methods that are private to the parent class with the same name. Just as a new property or method of its own. However, the parameters of the method must be identical.
- The overriding problem with the constructor method: The constructor method can be overridden not just like other common methods, but more lenient than the normal method: the arguments can be inconsistent when overridden.
Final Class Final class:
Usually, a class, without special statements, " others " can be arbitrarily used and " extended "- inheritance.
But:
If a class does not want to extend it, it can be declared as a " final class ."
Form:
Final class class name { .... Class definition .... }
Final Approach Final Method
Typically, a method, if not specifically declared, can be overridden by a subordinate class .
But:
If a method does not want to be overridden by a subordinate class, it can be a " final Method" of its life .
Form:
Final function method name () {.... Method definition .... }
Design Patterns
What do you mean design mode
The so-called design pattern, is a number of problems to solve the " conventional approach "is a good experience to conclude. Faced with different problems, there may be different solutions, at this point can be called different design patterns.
Factory mode
In practice, we always need to instantiate a lot of classes -- to get objects.
The
We can design a " factory "(in fact, a class), the role of the plant (the task) is to " produce" a variety of objects for people. This kind of factory usually can get an object of this class as long as the class name is specified.
PHP Object-oriented 4 section (ii) inheritance