Blogger Information
Blog 24
fans 0
comment 1
visits 14818
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0502课后作业
张成钢的博客
Original
625 people have browsed it

job0502.png

实例

<?php 
	class car {
		//属性
		private $brand;
		private $type;
		private $color;
		private $size=[];

		//构造方法
		public function __construct($brand='',$type='',$color='',array $size=[]) 
		{
			$this->brand = $brand;
			$this->type = $type;
			$this->color = $color;
			$this->size = $size;
		}

		//__get魔术方法 查询器
		public function __get($property_name)
		{
			$msg = null;
			if (isset($this->$property_name)){
				$msg = $this->$property_name;
			}
			else {
				$msg = '属性'.$property_name.'不存在!';
			}
			return $msg;
		}


		//设置器
		public function __set($property_name,$value)
		{
			$this->$property_name = $value;
		}
	}

 ?>

运行实例 »

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

实例

<?php 
	require './class/car.php';
	$newcar = new car('宝马','X5','白色',[4909,1938,1772]);

	// get测试
	echo '品牌:'.$newcar->brand.'<br>';
	echo '型号:'.$newcar->type.'<br>';
	echo '颜色:'.$newcar->color.'<br>';
	echo '尺寸: '. print_r($newcar->size,true). '<br>';

	echo 'GPS:'.$newcar->gps.'<br>';

	echo '<hr>';
	// set测试
	$newcar->brand = '奥迪';
	$newcar->type = 'A6';
	$newcar->color = '黑色';
	$newcar->size = [5012,1855,1485];

	echo '品牌:'.$newcar->brand.'<br>';
	echo '型号:'.$newcar->type.'<br>';
	echo '颜色:'.$newcar->color.'<br>';
	echo '尺寸: '. print_r($newcar->size,true). '<br>';

 ?>

运行实例 »

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


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!