PHP入门教程之面向对象的特性分析(继承,多态,接

<?php header('Content-Type:text/html; charset=utf-8;'); //创建一个电脑类 class Computer { //什么叫做类内,就是创建类的花括号内的范围叫做类内,其他地方则类外。 //public 是对字段的公有化,这个字段类外即可访问,赋值和取值 public $_name = '联想'; } $computer = new Computer(); $computer -> _name = 'Dell'; echo $computer->_name; ?>

Demo2.php

<?php header('Content-Type:text/html; charset=utf-8;'); class Computer { //private 是私有化,即对字段进行封装的操作,类外无法访问,取值和赋值都不能操作 private $_name = '联想'; } $computer = new Computer(); echo $computer->_name; ?>

Demo3.php

<?php header('Content-Type:text/html; charset=utf-8;'); class Computer { private $_name = '联想'; //这个时候我采用一个公共对外的方法来访问私有字段 //因为私有字段只能在类内访问,而对外的公共方法是类内的。 //更而公共方法又是公共的,所以类外又可访问。 public function _run(){ //字段在类内调用的时候必须是类 -> 字段,而$_name只是一个普通变量而已。 //字段在类外调用的方法是对象 -> 字段,而类内就必须使用 Computer -> _name //但是在本类中,可以使用一个关键字来代替字来代替 Computer ,那就是 $this echo $this ->_name; } } $computer = new Computer(); $computer -> _run(); ?>

Demo4.php

<?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) { //这里的 $name 只是一个变量而已,参数而已 //$this->name 才是类的字段 $this->name = $name; } } $computer = new Computer (); echo $computer->getName(); $computer->setName('Dell'); echo $computer->getName(); ?>

Demo5.php

<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { private $_name; private $_model; private $_cpu; //当类外的对象直接调用私有字段时,会跟着去检查是否有拦截器, //如果直接对 $_name 进行赋值,那么__set 方法就会拦截住,就不会报错了。 //采用拦截器进行赋值和取值 //赋值 private function __set($_key,$_value){ //采用$_key = '_name',那么 $_value = '联想'; //$this ->_name = '联想'; $this ->$_key = $_value; } //取值 private function __get($_key){ return $this -> $_key; //如果 $_key = '_name' 那么 $this -> _name; //如果 $_key = '_cpu' 那么 $this -> _cpu; //如果 $_key = '_model' 那么 $this -> _model; } } $computer = new Computer (); $computer->_name = '联想'; $computer->_cpu = '四核'; $computer->_model = 'i7'; echo $computer->_name; echo $computer->_cpu; echo $computer->_model; ?>

Demo6.php

<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { private $_name; private $_model; private $_cpu; //__set 和 __get 方法私有了,还是可以执行,是因为 //因为目前程序的指针已经在类内了。而类内可以执行封装的方法 //类内执行私有方法,不会出现任何错误。 //它只需要间接的拦截就可以了。拦截是在内类执行的。 //说白了,__set() 和 __get() 是 PHP 内置的方法,具有一定的特殊性 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

<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { const NAME = 'DELL'; } //常量的输出方法 类::常量 echo Computer::NAME; //DELL ?>

Demo8.php

<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { public $_count = 0; public function _add(){ $this -> _count++; //$_count = $_count+1 $_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

<?php header ( 'Content-Type:text/html; charset=utf-8;' ); class Computer { public static $_count = 0; public function _add(){ //如果是静态成员字段,那么就应该用 self 来调用,而不是 $this 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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/db259a6f772184abd80483b484a5b786.html