这篇文章主要介绍了PHP入门教程之面向对象的特性,结合实例形式分析了php面向对象所涉及的继承、多态、接口、抽象类及抽象方法等,需要的朋友可以参考下
本文实例讲述了PHP面向对象的特性。分享给大家供大家参考,具体如下:
Demo1.php
1 2 3 4 5 6 7 8 9 10 11 12 | <?php
header('Content-Type:text/html; charset=utf-8;');
class Computer {
public $_name = '联想';
}
$computer = new Computer();
$computer -> _name = 'Dell';
echo $computer ->_name;
?>
|
登录后复制
Demo2.php
1 2 3 4 5 6 7 8 9 | <?php
header('Content-Type:text/html; charset=utf-8;');
class Computer {
private $_name = '联想';
}
$computer = new Computer();
echo $computer ->_name;
?>
|
登录后复制
Demo3.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php
header('Content-Type:text/html; charset=utf-8;');
class Computer {
private $_name = '联想';
public function _run(){
echo $this ->_name;
}
}
$computer = new Computer();
$computer -> _run();
?>
|
登录后复制
Demo4.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
private $name ;
private $model ;
private $cpu ;
private $keyboard ;
private $show ;
private $zb ;
public function getName() {
return $this ->name;
}
public function setName( $name ) {
$this ->name = $name ;
}
}
$computer = new Computer ();
echo $computer ->getName();
$computer ->setName('Dell');
echo $computer ->getName();
?>
|
登录后复制
Demo5.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
private $_name ;
private $_model ;
private $_cpu ;
private function set( $_key , $_value ){
$this -> $_key = $_value ;
}
private function get( $_key ){
return $this -> $_key ;
}
}
$computer = new Computer ();
$computer ->_name = '联想';
$computer ->_cpu = '四核';
$computer ->_model = 'i7';
echo $computer ->_name;
echo $computer ->_cpu;
echo $computer ->_model;
?>
|
登录后复制
Demo6.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
private $_name ;
private $_model ;
private $_cpu ;
private function set( $_key , $_value ) {
$this -> $_key = $_value ;
}
private function get( $_key ) {
return $this -> $_key ;
}
}
$computer = new Computer ();
$computer ->_name = '联想';
$computer ->_cpu = '四核';
$computer ->_model = 'i7';
echo $computer ->_name;
echo $computer ->_cpu;
echo $computer ->_model;
?>
|
登录后复制
Demo7.php
1 2 3 4 5 6 7 8 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
const NAME = 'DELL';
}
echo Computer::NAME;
?>
|
登录后复制
Demo8.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
public $_count = 0;
public function _add(){
$this -> _count++;
}
}
$computer1 = new Computer();
$computer1 ->_add();
$computer1 ->_add();
$computer1 ->_add();
echo $computer1 -> _count;
echo '<br />';
$computer2 = new Computer();
$computer2 ->_add();
$computer2 ->_add();
$computer2 ->_add();
echo $computer2 -> _count;
?>
|
登录后复制
Demo9.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
public static $_count = 0;
public function _add(){
self:: $_count ++;
}
}
$computer1 = new Computer();
$computer1 ->_add();
echo Computer:: $_count ;
$computer1 ->_add();
echo Computer:: $_count ;
$computer1 ->_add();
echo Computer:: $_count ;
echo '<br />';
$computer2 = new Computer();
$computer2 ->_add();
echo Computer:: $_count ;
$computer2 ->_add();
echo Computer:: $_count ;
$computer2 ->_add();
echo Computer:: $_count ;
?>
|
登录后复制
Demo10.php
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
public static $_count = 0;
public static function _add(){
self:: $_count ++;
}
}
Computer::_add();
Computer::_add();
Computer::_add();
echo Computer:: $_count ;
?>
|
登录后复制
Demo11.php
1 2 3 4 5 6 7 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
}
$computer = new Computer();
echo $computer instanceof Computer;
?>
|
登录后复制
Demo12.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
public $_name = '联想';
public function _run(){
echo '联想在运行!';
}
}
class NoteComputer extends Computer {
}
$noteComputer = new NoteComputer();
echo $noteComputer -> _name;
$noteComputer -> _run();
?>
|
登录后复制
Demo13.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
public $_name = '联想';
public function _run(){
echo '联想在运行!';
}
}
class NoteComputer extends Computer {
public $_name = 'Dell';
public function _run(){
echo 'Dell在运行!';
}
}
$noteComputer = new NoteComputer();
echo $noteComputer -> _name;
$noteComputer -> _run();
?>
|
登录后复制
Demo14.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
protected $_name = '联想';
protected function _run(){
return '联想在运行!';
}
}
class NoteComputer extends Computer {
public function getTop() {
echo $this ->_name;
echo $this ->_run();
}
}
$noteComputer = new NoteComputer();
$noteComputer -> getTop();
?>
|
登录后复制
Demo15.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
public $_name = '联想';
public function _run(){
return '联想在运行!';
}
}
class NoteComputer extends Computer {
public $_name = 'Dell';
public function _run(){
echo 'Dell在运行!';
echo parent :: _run();
}
}
$noteComputer = new NoteComputer();
echo $noteComputer -> _name;
$noteComputer -> _run();
?>
|
登录后复制
Demo16.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
class Computer {
final public function _run(){
}
}
class NoteComputer extends Computer {
public function _run(){
}
}
$noteComputer = new NoteComputer();
?>
|
登录后复制
Demo17.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
abstract class Computer {
public $_name = '联想';
abstract public function _run();
public function _run2(){
echo '我是父类的普通方法';
}
}
class NoteComputer extends Computer {
public function _run(){
echo '我是子类的方法';
}
}
$noteComputer = new NoteComputer();
$noteComputer -> _run();
$noteComputer -> _run2();
echo $noteComputer -> _name;
?>
|
登录后复制
Demo18.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
interface Computer {
const NAME = '成员 ';
public function _run();
public function _run2();
}
interface Computer2 {
public function _run3();
}
class NoteComputer implements Computer,Computer2 {
public function _run() {
echo '我重写了run';
}
public function _run3() {
echo '我重写了run3';
}
public function _run2() {
echo '我重写了run2';
}
}
$noteComputer = new NoteComputer();
$noteComputer -> _run();
$noteComputer -> _run2();
$noteComputer -> _run3();
echo NoteComputer::NAME;
?>
|
登录后复制
Demo19.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <?php
header ( 'Content-Type:text/html; charset=utf-8;' );
interface Computer {
public function version();
public function work();
}
class NoteComputer implements Computer {
public function version() {
echo '笔记本';
}
public function work() {
echo '可以便携式运行 win7';
}
}
class DesktopComputer implements Computer {
public function version() {
echo '台式机';
}
public function work() {
echo '在工作站运行 XP';
}
}
class Person {
public function _run( $type ) {
echo '这个人的';
$type -> version();
$type ->work();
}
}
$noteComputer = new NoteComputer();
$desktopComputer = new DesktopComputer();
$person = new Person();
$person -> _run( $noteComputer );
?>
|
登录后复制
以上是php面向对象中的继承,多态,接口,抽象类,抽象方法实例教程的详细内容。更多信息请关注PHP中文网其他相关文章!