search
Homephp教程PHP开发php面向对象编程之类的继承性

这一章中我们主要讲一下关于类的继承性。


什么是类的继承性?

继承性是面向对象程序设计的重要特性之一,它是指建立一个新的派生类,从一个先前定义的类中继承属性和方法,而且可以重新定义或者加进新的属性和方法以建立类的层次或等级关系。简单地说继承性就是就是通过子类对父类进行功能扩展,继承而产生的类叫做子类,被继承的类叫做父类或超类。



php中类的继承需要注意的地方

1、一个类只能直接从另一个类中继承属性和方法,但一个类可以有多个子类。

2、子类不能继承父类的私有属性和私有方法。

3、在PHP5中类的方法可以被继承,类的构造函数也能被继承。

php中类的继使用关键字"extends"实现多个类的单继承关系,下面我们通过一个实例来演示一下父类和子类之间的层次关系。

首先我们来定义一个Person类作为父类,Person类具有人的基本属性如名字,性别和年龄以及说话和走路等行为。看代码:

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment