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 of classes
Inheritance, as one of the three important features of object-oriented, plays an extremely important role in the field of object-oriented.
I don’t seem to have heard of it. Object-oriented languages do not support inheritance. Inheritance is one of the important features of PHP5 object-oriented programming. 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 the 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 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". This class has
two member attributes "name and age" and two
member methods
"talking method and walking method" ", if the program now needs a student class, because students are also people, so students also have member attributes "name and age" and member methods "talking method and walking method", at this time you You can let the student class inherit this class. After inheritance, the student class will inherit all the attributes in the human being, so you don't need to redeclare these member attributes
and methods, because The student class also has the attributes of the school and the learning methods, so in the student class you make, there are
attributes and methods
inherited from humans, plus the student-specific "location" School Attributes" and "Learning Methods",
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, On the basis of the original two attributes and two methods in human, add one attribute and one method to extend a new student class. Through the inheritance mechanism, existing
data types
can be used to define new data types. The new data type
defined not only has newly defined members, but also has old members. We call existing classes used to derive new classes as base
classes, also known as parent classes and super classes. 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 standardized hierarchical structure of classes. Through the inheritance relationship of classes, public features can be shared, improving the reusability of software.
In the 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 the abstraction of the "human" class
Code snippet
//定义一个“人”类作为父类 class Person{ //下面是人的成员属性 var $name; //人的名子 var $sex; //人的性别 var $age; //人的年龄 //定义一个 构造方法 参数为属性姓名$name、性别$sex和年龄$age进行赋值 function construct($name, $sex, $age){ $this->name=$name; $this->sex=$sex; $this->age=$age; } //这个人可以说话的方法, 说出自己的属性 function say() { echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."<br>"; } }
//定义一个“人”类做为父类 class Student{ //下面是人的成员属性 var $name; //人的名子 var $sex; //人的性别 var $age; //人的年龄 var $school; //学生所在学校的属性 //定义一个构造方法参数为属性姓名$name、性别$sex和年龄$age进行赋值 function construct($name=””, $sex=””, $age=””, $school=””){ $this->name=$name; $this->sex=$sex; $this->age=$age; $this->school=$school; } //这个人可以说话的方法, 说出自己的属性 function say() { echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."<br>"; } //这个学生学习的方法 function study() { echo "我的名子叫:".$this->name." 我正在”.$this->school.”学习<br>"; } } //定义一个子类“学生类“使用”extends”关键字来继承”人”类 class Student extends Person{ var $school; //学生所在学校的属性 //这个学生学习的方法 function study() { echo "我的名子叫:".$this->name." 我正在”.$this->school.”学习<br>"; } }
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( );
The use of the above class inheritance simplifies the creation workload of objects and classes and increases the reproducibility of the code. However, from the example above, the impact of "reusability" and other inheritances 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 With a little effort, these properties and methods can all be inherited from the "Person" human being.
The above is the detailed content of Detailed explanation of PHP object-oriented inheritance examples. For more information, please follow other related articles on the PHP Chinese website!