Home  >  Article  >  Backend Development  >  PHP object-oriented guide (7) Inheritance_PHP tutorial

PHP object-oriented guide (7) Inheritance_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:43:56701browse

11. Class Inheritance
As one of the three important features of object-oriented, inheritance plays an extremely important role in the object-oriented field.
I don’t seem to have heard of any object-oriented language. Inheritance is not supported. Inheritance is one of the important features of object-oriented programming in PHP5. It refers to creating a new derived class that inherits data and functions from one or more previously defined classes, and can be redefined or added. Enter new data and functions, thereby establishing a hierarchy or hierarchy of classes. To put it simply, inheritance is a mechanism by which child
classes automatically share the data structures and methods of the parent class. This is a relationship between classes. When defining and implementing
of a class, you can do it on the basis of an existing class, use the content defined by the existing class as
's own content, and add some new content. For example, you now have a class "person", which contains
two member attributes "name and age" and two member methods "how to speak and how to walk". If now
The program needs a student class, because students are also people, so students also have member attributes "name and age" and member
member methods "how to talk and how to walk". At this time, you can let the student class come After inheriting this class, the
student class will inherit all the attributes in the human being. You don’t need to re-declare these member attributes
and methods, because there are still places in the student class. The attributes of the school and the methods of learning, so in the student class you make, there are
In addition to the attributes and methods inherited from humans, plus the unique "school attributes" and "learning methods" unique to the student,
Such a student class is declared. Inheritance can also be called "extension". From the above we can see that the student
class extends human beings. There are two original attributes and two methods in human beings. On the basis of adding an attribute and a method
, a new student class is extended.
Through the inheritance mechanism, existing data types can be used to define new data types. The new data type
defined not only has the newly defined members, but also has the old members. We call the existing class used to derive new classes the base
class, also known as the parent class and super class. A new class derived from an existing class is called a derived class, also called a subclass.
In software development, the inheritance of classes makes the software created open and extensible. This is an effective method of organizing and classifying information. It simplifies the creation of objects and classes. workload, increasing the reproducibility of the code. Using inheritance,
provides a canonical hierarchical structure of classes. Through the inheritance relationship of classes, public features can be shared, improving the reusability of software.
In C++ language, a derived class can be derived from one base class or multiple base classes. Inheritance derived from one base class is called single inheritance; inheritance derived from multiple base classes is called multiple inheritance.
But there is no multiple inheritance in PHP and Java languages, only single inheritance. That is to say, a class can only inherit data directly from
one class. This is what we call single inheritance.
For example:
The following is an abstraction of the "human" class
Code snippet



Copy the code

The code is as follows: //Define a "person" class as the parent classclass Person{
//The following are the member attributes of the person
var $name; //The person's name
var $sex ; //Person's gender
var $age; //Person's age
//Define a constructor method parameter to assign values ​​to the attributes name $name, gender $sex and age $age
function __construct($ name, $sex, $age){
$this->name=$name;
$this->sex=$sex;
$this->age=$age;
}
//The way this person can speak, tell his own attributes
function say() {
echo "My name is: ".$this->name." Gender: ".$this->sex." My age is: ".$this->age."
";
}
}


Here we are Make a "student class", if inheritance is not used as follows: Code snippet


Copy code
The code is as follows:

//Define a "human" class as the parent class
class Student{
//The following are the member attributes of the person
var $name; //The person's name
var $sex; //Person’s gender
var $age; //Person’s age
var $school; //Attribute of the school where the student is located
//Define a constructor parameter as attribute name$ name, gender $sex and age $age are assigned
function __construct($name="", $sex="", $age="", $school=""){
$this-> name=$name;
$this->sex=$sex;
$this->age=$age;
$this->school=$school;
}
//How this person can speak, tell his own attributes
function say() {
echo "My name is: ".$this->name." Gender: ".$this ->sex." My age is: ".$this->age."
";
}
//How this student learns
function study() {
echo "My name is: ".$this->name." I am studying at ".$this->school."
";
}
}
//Define a subclass "student class" using the "extends" keyword to inherit the "person" class
class Student extends Person{
var $school; //Attributes of the school where the student is located
//This Methods for students to study
function study() {
echo "My name is: ".$this->name." I am studying at ".$this->school."
";
}
}

Through the definition of the "Student" class above, the Student class uses the "extends" keyword to combine all member attributes in the Person class
with All member methods are inherited, and a member attribute "school" of the school is extended, and
a study method "study()". Now the subclass "Student" and the objects created using this class instance have the following
attributes and methods:
The member attributes in the student class "Student" are:
Name: name;
Age: age;
Gender: sex;
School: school;
The member methods in the student class "Student" are:
Speaking method: say();
Learning method: study( );
Through the use of the above class inheritance, the workload of creating objects and classes is simplified, and the reproducibility of the code is increased. However, from the above example, the impact of "reusability" and other inheritance is not particularly obvious. If you think about it more broadly, people have There are countless positions, such as the students above, teachers, engineers, doctors, workers
, etc., there are many, many. If each class defines attributes and methods that "people" have in common, think about it, there will be a lot of Work
, these properties and methods can be inherited from the "Person" human being.

http://www.bkjia.com/PHPjc/320641.html

truehttp: //www.bkjia.com/PHPjc/320641.htmlTechArticle11. Class Inheritance As one of the three important characteristics of object-oriented, inheritance has a great influence in the field of object-oriented. It plays an extremely important role. It seems that I have never heard of any object-oriented language that does not support it...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn