11. class inheritance
Inheritance, as one of the three important features of object-oriented, plays an extremely important role in the Object-Oriented field,
It seems that I have never heard of any object-oriented language that does not support inheritance. Inheritance is an important feature of PHP5 Object-Oriented Programming
1. It refers to creating a new derived class that inherits data and functions from one or more previously defined classes, and can
Redefine or add new data and functions to create a class hierarchy or level. Simply put, the inheritance is sub-
The class automatically shares the data structure and method mechanism of the parent class, which is a relationship between classes. When defining and implementing a class
You can base an existing class and take the content defined by this existing class
And add some new content. For example, you already have a "person" class, which contains
Two member attributes: "name and age" and two member Methods: "speaking method and walking method ".
The program requires a student class. Because the student is also a person, the student also has the member attribute "name and age" and
The employee method is "speaking and walking". At this time, you can let the student class inherit the class. After the inheritance,
Students will inherit all the attributes of humans, so you don't have to re-declare these member attributes.
And method, because the student class also has the attributes of the school and the learning method, so in your student class there are
In addition to attributes and methods inherited from humans, the unique "school attributes" and "learning methods" of students are added ",
Such a student class is declared complete, and inheritance can also be called "extension". From the above we can see that students
Class extends humans, adding an attribute and a method on the basis of the original two attributes and two methods in humans
Expand a new student category.
The Inheritance Mechanism allows you to use existing data types to define new data types. New data type defined
Not only members with new definitions, but also old members. We call the existing classes used to derive new classes as the basis.
Class, also known as parent class and superclass. A new class derived from an existing class is called a derived class, or a subclass.
In software development, the inheritance of classes makes the established software open and scalable. This is information organization and analysis.
Class, which simplifies the creation workload of objects and classes and increases the code reusability. Use inheritance,
Provides a standard class level structure. Through the inheritance relationship of classes, public features can be shared, improving the software's importance.
Usability.
In C ++, a derived class can be derived from a base class or multiple base classes. Inheritance derived from a base class is called single inheritance; inheritance derived from multiple base classes is called multi-inheritance.
However, in PHP and Java, there is not much inheritance, but only single inheritance. That is to say, a class can only be directly inherited from
A class inherits data, which is what we call single inheritance.
For example:
The following is the abstraction of the "person" class.
Code snippet
Copy codeThe Code is as follows: // define a "person" class as the parent class
Class Person {
// The following are the member attributes of a person.
Var $ name; // name of a person
Var $ sex; // gender of a person
Var $ age; // age of a person
// Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age.
Function _ construct ($ name, $ sex, $ age ){
$ This-> name = $ name;
$ This-> sex = $ sex;
$ This-> age = $ age;
}
// This person can talk about his/her attributes.
Function say (){
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. "<br> ";
}
}
Next we will create a "Student Class". If it is not inherited as follows: code snippetCopy codeThe Code is as follows: // define a "person" class as the parent class
Class Student {
// The following are the member attributes of a person.
Var $ name; // name of a person
Var $ sex; // gender of a person
Var $ age; // age of a person
Var $ school; // attributes of the student's school
// Define a constructor parameter to assign values to the attribute name $ name, gender $ sex, and age $ age.
Function _ construct ($ name = ", $ sex =", $ age = ", $ school = ""){
$ This-> name = $ name;
$ This-> sex = $ sex;
$ This-> age = $ age;
$ This-> school = $ school;
}
// This person can talk about his/her attributes.
Function say (){
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. "<br> ";
}
// How the student learns
Function study (){
Echo "My name is:". $ this-> name. "I am". $ this-> school. "Learn <br> ";
}
}
// Define a subclass "Student Class" using the "extends" keyword to inherit "person" Class
Class Student extends Person {
Var $ school; // attributes of the student's school
// How the student learns
Function study (){
Echo "My name is:". $ this-> name. "I am". $ this-> school. "Learn <br> ";
}
}
Through the definition of the "Student" class above, the Student class uses the keyword "extends" to set the Person class
All member attributes and member methods in, and extend the attributes of a school member "school", and
One learning method is "study ()". The objects in the subclass "Student" and the instances using this class have the following
Attributes and methods:
The member attributes in Student include:
Name: name;
Age: age;
Gender: sex;
School: school;
The member methods in Student include:
Method of speaking: say ();
Learning Method: study ();
The use of class inheritance simplifies the creation workload of objects and classes, and increases the code reusability. However
In this example, the impact of "reusability" and other inheritance is not particularly clear.
Obviously, you can expand your mind to think about whether there are several posts for people, such as the above students, teachers, engineers, doctors, and workers.
And so on. If every class defines the attributes and methods that "people" share, it will be a great job to think about it.
These attributes and methods can be inherited from "Person" humans.