日志标签:面向对象练习

整理:类和对象的练习代码

分类:网站技术日期:2010-07-22 - 8:29:53评论:0条作者:老谢

class person
{
	var $name;
	var $sex;
	var $age;
	function say()
	{
		echo "说话";
	}
	function run()
	{
		echo "跑步";
	}
}
$p1=new person();
$p2=new person();
$p3=new person();
$p1->name="张三";
$p1->sex="男";
$p1->age="20";
echo "p1的名字:".$p1->name."<br>";
echo "p1的性别:".$p1->sex."<br>";
echo "p1的年龄:".$p1->age."<br>";
 
class diy
{
	var $cpu;
	var $ram;
	var $chipset;
	function run()
	{
		echo "这台电脑可以运行"."<br>";
	}
}
$p1=new diy;
$p2=new diy;
$p1->cpu="8650";
$p1->ram="1GB";
$p1->chipset="770";
echo "CPU型号:".$p1->cpu."<br>";
echo "内存大小:".$p1->ram."<br>";
echo "芯片组:".$p1->chipset."<br>";
$p1->run();
$p2=new diy;
$p2=new diy;
$p2->cpu="X2 720";
$p2->ram="2GB";
$p2->chipset="770";
echo "cpu的型号:".$p2->cpu."<br>";
echo "内存容量:".$p2->ram."<br>";
echo "芯片组的型号:".$p2->chipset."<br>";
$p2->run();
 
class person
{
	var $name;
	var $sex;
	var $age;
	function say()
	{
		echo "我的名字是:".$this->name."&nbsp"."性别:".$this->sex."&nbsp"."年龄:".$this->age."<br>";
	}
	function run()
	{
		echo "这个人在走路";
	}
}
$p1=new person();
$p2=new person();
$p3=new person();
$p1->name="张三";
$p1->sex="男";
$p1->age="20";
$p1->say();
$p2->name="李四";
$p2->sex="男";
$p2->age="18";
$p2->say();
$p3->name="张芳";
$p3->sex="女";
$p3->age="15";
$p3->say();
 
class person
{
	var $name;
	var $sex;
	var $age;
}
	function __construct($name, $sex, $age) 
	{
		$this->name=$name;
		$this->sex=$sex;
		$this->age=$age;
	}
	function say()
	{
		echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."<br>"; 
	}
$p1=new person("张三","男",20);
$p2=new person("李四","男",22);s
$p3=new person("王五","男",30);
$p1->say();
 
	class diy
{
	var $cpu;
	var $ram;
	var $chipset;
	function config()
	{
		echo "cpu型号:".$this->cpu."&nbsp"."内存容量:".$this->rma."&nbsp"."芯片组:".$this->chipset."<br>";
	}
}
$p1=new diy;
$p2=new diy;
$p1->cpu="8650";
$p1->ram="2GB";
$p1->chipset="770";
$p2->cpu="9650";
$p2->ram="4GB";
$p2->chipset="770";
$p1->config();
$p2->config();
 
class test
{
	var $x;
	var $y;
	var $z;
	function s_um($x,$y)
	{
		$this->x=$x;
		$this->y=$y;
		$this->z=$x+$y;
		echo "它们的和是:".$this->z."<br>";
	}
}
$p1=new test;
$p1->s_um(10,10);
$p2=new test;
$p2->s_um(1,1);
 
class test
{
	var $name;
	var $sex;
	var $age;
	function say()
	{
		echo "我的名字是:".$this->name." 年龄是:".$this->age." 性别是:".$this->sex."<br>";
	}
}
$p1=new test;
$p2=new test;
$p3=new test;
$p1->name="张三";
$p1->age="20";
$p1->sex="男";
$p2->name="李四";
$p2->age="18";
$p2->sex="男";
$p1->say();
$p2->say();
 
class diy
{
	var $cpu;
	var $ram;
	function config($cpu,$ram)
	{
		$this->cpu=$cpu;
		$this->ram=$ram;
		echo $this->cpu."<br>".$this->ram;
	}
}
$p1=new diy;
$p1->config("型号:8650","内存大小:2G");