您的位置:首页 > 编程语言 > PHP开发

继承+关键字(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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  class 函数 php