继承+关键字(static+const+instanceof+final+)+protected
2017-01-16 22:59
323 查看
extends Person.class.php Student.class.php Teacher.class.php Person.class.php----------------------------------- <?php class Person{ public $name = "tom"; function walk(){ echo "I can walk"; } } Student.class.php------------------------------------ <?php //继承语法应用举例 include "Person.class.php"; class Student extends Person{ public $school="2"; //子类重载父类中的成员方法walk function walk(){ echo "I am student".parent::walk(); } function study(){ echo $this->name; echo "<hr/>"; $this->walk(); echo "I can study"; } } $s = new Student; //$s->study(); $s->walk(); Teacher.class.php----------------------------------- <?php /* * 3、定义父类Person.class.php, * 定义子类Teacher.class.php,在父类 中有成员属性$name = "tom", 有成员方法walk(),输出内容“I can walk”, 在子类中继承父类Person,在实例化子类对象, 访问父类中的成员属性和成员方法。观察输出。 */ include 'Person.class.php'; class Teacher extends Person{ } $t = new Teacher; echo $t->name; echo "<hr/>"; $t->walk(); ---------------------------------------------------- final文件 Person.class.php Student.class.php Person.class.php----------------------------------- <?php //final class Person{ //被final修饰的类不能被继承。 class Person{ //final $name; final function walk(){ // 被final修饰的成员 //方法不能够被重载。 echo "I can walk"; } } Student.class.php----------------------------------- <?php include 'Person.class.php'; class Student extends Person{ function walk(){//这个成员方法父类已经final了,不能够访问 echo "I am a student I can walk"; } } ---------------------------------------------------- instanceof文件 Person.class.php Student.class.php Teacher.class.php Person.class.php------------------------------------ <?php class Person{ } Student.class.php----------------------------------- <?php include 'Person.class.php'; class Student extends Person{ } //子类是父类的实例,可以向上 $s = new Student; $p = new Person; if($s instanceof Person){ echo "yes";//yes }else{ echo "no"; } echo "<hr/>"; //父类不是子类的实例,不可以向下 if($p instanceof Student){ echo "yes"; }else{ echo "no";//no } Teacher.class.php----------------------------------- <?php /* * 1、定义Person 类,定义Teacher类继承Person类, * 实例化Teacher类, 判断Teacher类的类对象是否属 * 于Person类。 如果属于,则输出 yes,否则输出no */ include 'Person.class.php'; class Teacher extends Person{ } $t = new Teacher; if($t instanceof Person){ echo "yes"; }else{ echo "no"; } ---------------------------------------------------- const文件 const.php define.php Person.class.php const.php--------------------------------------- <?php const DNS="www.mysite.com"; echo DNS; define.php--------------------------------------- <?php //define 常量定义应用举例 /* * - 常量名称尽量用大写 * - 常量一经定义不能改变 */ define("DNS","www.mysite.com"); define("ROOT","/opt/module/www"); echo DNS; Person.class.php-------------------------------- <?php //常量定义 /* * 2、在Person类中声明常量COUNTRY, * 试着在类里和类外访问常量 COUNTRY */ class Person{ //define 定义常量不能在类里面使用。 //命名空间不支持define //define("DNS","www.mysite.com"); //定义常量 const COUNTRY="china"; function say(){ echo self::COUNTRY; } } echo Person::COUNTRY; echo "<hr/>"; $p = new Person; $p->say(); ---------------------------------------------------- static文件 Person.class.php Person.class.php--------------------------------- <?php //static class Person{ //static不能用来声明类 /* * 3、在Person中声明被static修饰的成员属性$country, * 在类里和类外分别对成员属性$country进行访问。 */ class Person{ static $country="china"; public $name; static function say(){ //在静态化方法中不能够访问非静态的成员属性和方法 //$this->test();//非法 //$this->test();//非法 echo self::$country; } function test(){ //self::say(); } } echo Person::$country; echo "<hr/>"; /*$p = new Person; $p->say();*/ //在类外访问static修饰的成员方法 Person::say(); echo "<hr/>"; $p = new Person; $p->test(); ---------------------------------------------------- protected文件 Person.class.php Student.class.php Person.class.php--------------------------------- <?php class Person{ protected $name="tom"; public $age=10; private $height="1"; protected function say(){ echo "I can say"; } function walk(){ $this->say(); echo $this->name; } private function test(){ } } //$p = new Person; //$p->name; //被protected修饰的成员属性在类外不能被访问 //$p->say();//被protected修饰的成员方法在类外不能被访问 //$p->walk(); ---------------------------------------------------- <?php include 'Person.class.php'; class Student extends Person{ //在Student类中访问父类中被protected //修饰的成员属性 function study(){ /*在Student类中访问父类中被private *修饰的成员属性 */ //echo $this->height; //在子类中不能访问父类中被private修饰的成员属性 /*在Student类中访问父类中被private *修饰的成员方法 */ //echo $this->test();//在子类中不能访问父类中被private修饰的成员方法 echo "<hr/>"; /*在Student类中访问父类中被public *修饰的成员属性 */ echo $this->age; /*在Student类中访问父类中被public *修饰的成员方法 */ $this->walk(); echo "<hr/>"; echo $this->name; //在Student类中访问父类中被protected //修饰的成员方法 $this->say(); } } $s = new Student; $s->study(); Student.class.php------------------------------- <?php include 'Person.class.php'; class Student extends Person{ //在Student类中访问父类中被protected //修饰的成员属性 function study(){ /*在Student类中访问父类中被private *修饰的成员属性 */ //echo $this->height; //在子类中不能访问父类中被private修饰的成员属性 /*在Student类中访问父类中被private *修饰的成员方法 */ //echo $this->test();//在子类中不能访问父类中被private修饰的成员方法 echo "<hr/>"; /*在Student类中访问父类中被public *修饰的成员属性 */ echo $this->age; /*在Student类中访问父类中被public *修饰的成员方法 */ $this->walk(); echo "<hr/>"; echo $this->name; //在Student类中访问父类中被protected //修饰的成员方法 $this->say(); } } $s = new Student; $s->study();
相关文章推荐
- PHP 面向对象中常见关键字使用(final、static、const和instanceof)
- const,define,static,$GLOBALS,final关键字解释
- php面向对象类中的$this,static,final,const,self及双冒号 :: 这几个关键字使用方法。
- php关键字:final self static const
- php类中的$this,static,final,const,self这几个关键字使用方法
- php面向对象全攻略 (十) final static const关键字的使用
- final static const关键字的使用
- c# 继承 和 关键字:abstract,const,override,readonly,static,virtual,base,this
- php类中的$this,static,final,const,self这几个关键字使用方法
- abstract,final,const,self,static关键字的使用
- php中$this、static、final、const、self 等几个关键字的用法
- php中$this、static、final、const、self 等几个关键字的用法
- JAVA main方法详解、单例设计模式、继承、关键字(static、extends、super、instanceof、final)
- php入门学习-----const&static&final关键字
- 关于static静态的、继承、接口interface、final关键字、单例设计模式总结
- php怎么理解这些个关键字啊,public;private;protect;final;static;const
- java 继承、抽象、private、static、final关键字在类中的使用
- static和const,及final关键字的使用
- JAVA中的关键字static,final和const