PHP Object-oriented all-in-all strategy (vii) Inheritance _php tutorial

Source: Internet
Author: User
11. Inheritance of Classes
Inheritance, as an aspect of the three important features of object-oriented, plays an important role in the field of object-oriented.
I don't think I heard that. Object-oriented languages do not support inheritance. Inheritance is an important feature of PHP5 object-oriented programming
One, it refers to the creation of 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 hierarchies or levels of classes. The simple point is that inheritance is a child
Class automatically shares the mechanism of the data structure and methods of the parent class, which is a relationship between classes. In defining and implementing a class's
, it can be done on top of an already existing class, and the content defined by the already existing class as
own content and add a number of new content. For example, you now have a "human" class, and this class has
Two member properties "name and age" as well as two member methods "talking Method and walking method" if now
The program requires a student's class, because the student is also a person, so the student also has the member attribute "name and age" as well as the
Method of "Speaking and walking", this time you will be able to let the student class to heir this class, after inheriting,
The student class will inherit all the attributes of human beings, so you don't have to re-declare them again. These member properties
and methods, because there are students in the school's properties and learning methods, so in the student class you do have
In addition to the attributes and methods inherent in human beings, plus the student's unique "school attributes" and "Learning methods",
Such a student class is declared complete, inherited we can also be called "extension", from above we can see that students
Classes extend to humans, adding an attribute and a method based on the original two attributes and two methods in humans.
Extend out a new class of students.
The inheritance mechanism allows you to define new data types by using existing data types. The new data type that is defined
Not only have the newly defined members, but also have the old members. We call the existing class that derives the new class as the base
Class, also known as a parent class, and a superclass. A new class derived from an existing class is called a derived class, also known as a subclass.
In software development, the inheritance of the class makes the software open and extensible, which is the information organization and
class, which simplifies the creation of objects, classes, and increases the weight of the code. Adoption of inheritance,
Provides a hierarchical structure for the specification of a class. Through the inheritance of the class, the common features can be shared, and the software's weight is improved.
The use of sex.
In the C + + language, a derived class can derive from a base class or from more than one base class. Inheritance derived from a base class is called single inheritance, and inheritance derived from multiple base classes is called multiple inheritance.
However, in PHP and the Java language there is no multiple inheritance, only single inheritance, that is, a class can only be directly from the
Inherit data from a class, which is what we call single inheritance.
For example:
The following is an abstraction of the "human" class
Code Snippets
Copy CodeThe code is as follows:
Define a "People" class as the parent class
Class person{
The following is the person's member property
var $name; The name of the man
var $sex; Man's Sex
var $age; The age of the person
Define a constructor method parameter to assign a property name $name, gender $sex, and age $age
function __construct ($name, $sex, $age) {
$this->name= $name;
$this->sex= $sex;
$this->age= $age;
}
This person can speak in a way that speaks his own attributes
function say () {
echo "My name is called:". $this->name. "Gender:" $this->sex. "My Age is:". $this->age. "
";
}
}

Below we do a "student class", if not with inheritance as follows: Code snippet
Copy CodeThe code is as follows:
Define a "human" class as the parent class
Class student{
The following is the person's member property
var $name; The name of the man
var $sex; Man's Sex
var $age; The age of the person
var $school; The properties of the school where students are located
Define a constructor method parameter to assign a property 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 speak in a way that speaks his own attributes
function say () {
echo "My name is called:". $this->name. "Gender:" $this->sex. "My Age is:". $this->age. "
";
}
The way the student learns
Function study () {
echo "My name is called:". $this->name. "I am". $this->school. " Learn
";
}
}
Define a subclass "Student class" use the "extends" keyword to inherit the "People" class
Class Student extends person{
var $school; The properties of the school where students are located
The way the student learns
Function study () {
echo "My name is called:". $this->name. "I am". $this->school. " Learn
";
}
}

Through the definition of the "Student" class above, the Student class uses the keyword "extends" to put the person class
All member properties and member methods are inherited and extended to a school member attribute "school", and
A learning Method "study ()". Now the subclass "Student" and the objects that use this class instance have the following
The properties and methods of:
The member attributes of the student class "Student" are:
Name: name;
Age: Ages;
Gender: Sex;
School: School;
The member methods of the student class "Student" are:
Method of speech: Say ();
Learning Method: study ();
The use of the above class inheritance simplifies the creation of objects and classes, which increases the weight of the code. But from the top
"Reusability" and other inheritance in this example, we're not looking at a particular point
Show, you expand to think about, people have countless positions, such as the above students and teachers, engineers, doctors, workers
And so on, many, if each class defines the properties and methods that "people" share, think of a lot of work
, these properties and methods can be inherited from the "person" human.

http://www.bkjia.com/PHPjc/320641.html www.bkjia.com true http://www.bkjia.com/PHPjc/320641.html techarticle 11. Inheriting inheritance of classes as an aspect of the three important features of object-oriented is of great importance in the field of object-oriented, as if the object-oriented language has not been heard .

  • 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.