首页 | 新闻 | 交流 | 问吧 | 文档 | 手册 | 下载 | 博客

php面向对象比编程一节小笔记

作者:  时间: 2007-08-26


以下是今天刚学的两段代码,对于很多人来说应该很简单,这只是为了方便自己以后随时随地查阅而放在博客里的,被误导进来的朋友抱歉了。
1 创建类和对象
no=$arr["no"];
  $this->name=$arr["name"];
  $this->gender=$arr["gender"];
  $this->age=$arr["age"];
}
function grow($i)
{
  $this->age+=$i;
}
function get_data()
{
  echo "
";
  echo "学号:$this->no
";
  echo "姓名:$this->name
";
  echo "性别:$this->gender
";
  echo "年龄:$this->age
";
}
}
$s=new student;
$temp_arr=array("no"=>"001","name"=>"张海滨","gender"=>"男","age"=>22);
$s->set_data($temp_arr);
$s->get_data($temp_arr);
$s->grow(2);
$s->get_data();
?>
2重载新的方法
no=$arr["no"];
  $this->name=$arr["name"];
  $this->gender=$arr["gender"];
  $this->age=$arr["age"];
}
function grow($i)
{
  $this->age+=$i;
}
function get_data()
{
  echo "
";
  echo "学号:$this->no
";
  echo "姓名:$this->name
";
  echo "性别:$this->gender
";
  echo "年龄:$this->age
";
}
}
class colleage_student extends student
{
var $department;
function change_department($new_department)
{
  $this->department=$new_department;
}
function set_data($arr)
{
  $this->no=$arr["no"];
  $this->name=$arr["name"];
  $this->gender=$arr["gender"];
  $this->age=$arr["age"];
  $this->department=$arr["department"];
}
}
$s=new colleage_student;
$temp_arr=array("no"=>"001","name"=>"lijia","gender"=>"男","age"=>25,"department"=>"电子商务系");
$s->set_data($temp_arr);
$s->get_data();
echo "所在系:$s->department
";
$s->change_department("数学系");
echo "所在系:$s->department
";
?>