php--inheritance

Source: Internet
Author: User

inheritance Using keywords:extends
PHP is single-inheritance, and there is only one parent class
The constructor of PHP can be inherited, but if the subclass also defines a constructor, the parent class is overwritten

Subclasses define the functions of the parent class, called overriding

The parent keyword, which is intended to be a parent, is currently the object of the parent class of the current class in the child class.
Use it to invoke the properties and behavior of the overridden parent class

Class animal{

Public $name;
Public $sex;
Public $age;

Public function __construct ($name)
{
$this->name = $name;
}

Public function shout () {
echo ' Animals all have their own cries ';
}

Public Function desc () {

}
}
Class Dog extends animal{

Public function __construct ($name)
// {
$this->name = $name;
// }

/**
* Subclasses define the functions of the parent class, called Overrides
*/
Public function shout () {

Echo ', Dog's cry: Wang ~ ~ Wang ~ Wang ~;
}

Public Function desc () {
Echo ' Dog's name: '. $this->name;
parent::shout ();
$this->shout ();
}
}
Class Cat extends animal{

Public function __construct ($name)
// {
$this->name = $name;
// }

Public function shout () {
Echo ', the cat's cry: Meow ~ ~ Meow ~ ~ ~;
}
Public Function desc () {
Echo ' Meow's name: '. $this->name;
$this->shout ();
}
}

$animal = new Dog (' blessings ');
$animal->desc ();

Echo ' <br><br> ';

$animal = new Cat (' Mimi ');
$animal->desc ();

php--inheritance

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.