2025年php构造物品类和子类的代码,0613-PHP类构造方法、扩展和封装

php构造物品类和子类的代码,0613-PHP类构造方法、扩展和封装实例 1 创建一个对象的模板 没有实例化 this 谁实例化类 就是谁 他是一个对象 比如 student new students this 指的是 student parent 是指向父类的指针 一般我们使用 parent 来调用父类的构造函数 class

大家好,我是讯享网,很高兴认识大家。

实例

//1、创建一个对象的模板-----没有实例化

//$this :谁实例化类,就是谁,他是一个对象,比如$student =new students(),%this指的是$student

//parent :是指向父类的指针,一般我们使用parent来调用父类的构造函数。

class Students

{

//属性

public $name,$age,$gender,$bhw;

//方法

public function hobby($hobby){

return '他爱好是' . $hobby;

}

}

// 实例化,并赋值,可以直接赋值在字符串拼接里面

$student = new Students();   //实例化必须有

//$student->name = '小明';

//$student->age = 18;

//$student->gender = '男';

//$student->bhw = [10,20,30];

//$student->hobby('吃*');

//访问,字符串拼接

$res = '学生信息';

echo $res;

echo '


';

// 构造方法  ----  __construct

class Students_1

{

//属性

public $name,$age,$gender,$bhw;

//构造方法  __construct:调用立即执行

public function __construct($name,$age,$gender,$bhw)

{

$this->name=$name;

$this->age=$age;

$this->gender=$gender;

$this->bhw=$bhw;

// echo $this->getInfo();  //直接运行这里调用打印函数。

}

public function hobby($hobby){

return '他爱好是' . $hobby;  //函数必须返回数值

}

public function getInfo(){

$res = '父函数:学生信息';

return $res;  //必须给给类返回数值,不然$this->getInfo不能调用

}

}

$student1 = new Students_1('笑二','19','女',[20,20,20]);

//echo $student1->hobby('喝酒');

//单独输出getInfo()

echo $student1->getInfo();

echo '


';

//类的功能扩展:继承(继承一下class Students_2)

// 被扩展的类叫父类, 超类,或基类

// 扩展的类叫子类, 派生类

// 在扩展子类中, 父类中的public属性成员,可以直接使用

// 如果仅是简单的继承, 子类并没有添加新的属性或方法, 就失去了继承的意义

// 继承的本质是代码复用

class SubStudents_1 extends Students_1

{

private $ac;   //添加一个新属性:成绩

public function __construct($name,$age,$gender,$bhw,$ac)

{

//        $this->name=$name;

//        $this->age=$age;

//        $this->gender=$gender;

//        $this->bhw=$bhw;

//用parent 继承父类__construct的所有参数和内容,包括输出函数echo this->getInfo()

parent::__construct($name,$age,$gender,$bhw);

//增加一个学习成绩

$this->ac=$ac;

echo $this->getInfo();  //父函数和子函数只能有一个getInfo(),主函数里有经过继承就会运行主函数里面的getInfo()

}

public function hobby($hobby){

return '他爱好是' . $hobby;  //函数必须返回数值

}

//方法的扩展叫方法重写,必须重写,不能通过parent继承内容

public function getInfo(){

$res = '扩展函数:学生信息';


讯享网

return $res;  //必须给给类返回数值,不然$this->getInfo不能调用

}

}

$student2 = new SubStudents_1('马东明','29','未知',[31,22,60],48);

echo '


';

//封装技术

//将  public(公众属性) 改成 private(私有属性) 或者 protected(局部属性)

//先简单粗俗的描述下:

//public 表示全局,类内部外部子类都可以访问;

//private表示私有的,只有本类内部可以使用;

//protected表示受保护的,只有本类或子类或父类中可以访问;

class Students_2

{

//属性

public $name;

protected $age;

protected $bhw;

protected $gender;

//构造方法  __construct:调用立即执行

public function __construct($name,$age,$gender,$bhw)

{   //不想类外部查看三围 $bhw,设置$bhw为 private

$this->name=$name;

$this->age=$age;

$this->gender=$gender;

$this->bhw=$bhw;

echo $this->getInfo();  //直接运行这里调用打印函数。

}

public function hobby($hobby){

return '他爱好是' . $hobby;  //函数必须返回数值

}

public function getInfo(){

$res = '父函数:学生信息';

return $res;  //必须给给类返回数值,不然$this->getInfo不能调用

}

//想在外部访问类属性或函数,必须给他用函数做一个接口,来供外部访问

public function getsw(){

return  print_r($this->bhw,true);

}

}

$students_2 = new Students_2('马冬梅','5','女',[88,77,50]);

//访问不了 私有变量  echo $students_2->bhw;

//也访问不了受保护类型变量 echo $students_2->gender

//外部访问getsw()函数,public getsw()是全局属性,所以可以通过它间接访问$bhw三围数据。

echo '三围属性是:'.$students_2->getsw().'


';

//扩展上面的类,测试protected 使用方法

class subStudents_2 extends Students_2

{

//添加一个属性:漂亮程度和ID

private $id;

private $bui;

//父函数$age使用protected ,看这里是否能正常使用?看似有变量$bhw是否正常使用

public function __construct($name, $age, $gender, $bhw,$bui,$id)

{

parent::__construct($name, $age, $gender, $bhw);

$this->bui=$bui;

$this->id = $id;

//子类不能使用父类private 定义的$bhw

//$this->bhw=$bhw;

echo $this->getsw();

}

//重写获取三围函数

public function getsw(){

if ($this->id=== 001){

echo $this->name.'美丽度是:'.$this->bui;

}else{echo '你不是大象管理员,禁止观看大象美丽度';}

}

}

//用变量表示也行

$ = [

'id'=>001,

'name'=>'大象',

'age'=>88,

'gender'=>'公',

'bhw' => [77,88,99],

'bui' => 'AAAAA'

];

$subs_2 = new subStudents_2($['name'],$['age'],$['gender'],

$['bhw'],$['bui'],$['id']);

运行实例 »

点击 "运行实例" 按钮查看在线实例

小讯
上一篇 2025-02-16 15:24
下一篇 2025-02-20 23:28

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/66836.html