Php object-oriented full strategy (7) inheritance

Source: Internet
Author: User
Inheritance, as one of the three important features of object-oriented, plays an extremely important role in the object-oriented field. it seems that we have not heard of any object-oriented language that does not support inheritance. 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

The 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."
";
}
}


Next we will create a "student class". if it is not inherited as follows: code snippet

The 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."
";
}
// How the student learns
Function study (){
Echo "My name is:". $ this-> name. "I'm learning". $ this-> school ."
";
}
}
// 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'm learning". $ this-> school ."
";
}
}


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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.