类成员的访问限制案例之一

原创 2018-12-15 18:43:56 189
摘要:<?phpclass Stu{public $name; #公共的,都可以访问protected $parents; #受保护的,类内部和子类中可访问,外部不可访问private $address; #私有的,仅在内部可访问public function__construct($name, $parents, $address ){$this ->name=$name;$this -&

<?php

class Stu

{

public $name; #公共的,都可以访问

protected $parents; #受保护的,类内部和子类中可访问,外部不可访问

private $address; #私有的,仅在内部可访问

public function__construct($name, $parents, $address )

{

$this ->name=$name;

$this ->parents=$parents;

$this ->address=$address;

}

public fuction getParents()

{

$res= $this ->parents;

if ($this ->parents=='李四'){

$res='家长信息不可访问';

}

return $res;

}


$stu=new Stu('张三',‘李四’,‘东大行5号');

echo  '姓名是:' ,$stu->name;

echo '父母是:',$parents->parents;

echo '地址是',  $address->address;

?>

批改老师:天蓬老师批改时间:2018-12-15 19:49:08
老师总结:类成员,与普通代码成员的重要区别,就是访问限制,这不仅仅是作用域限制, 有了访问限制,就可以在上面进行很多特殊操作了,这是面向对象开发的基础

发布手记

热门词条