批改状态:合格
老师批语:
作业标题:0811 oop编程-1
作业内容:1. 请实例演绎你对面向对象类与对象关系的理解? 2. 请实例演绎oop的封装性是怎么实现的? 3. 请实例演绎构造函数的作用有哪些?
<?phpclass User{public $username;//类的属性protected $email;private $age;function job(){echo "I'm name is :".$this->username;}}$test=new User;//实例化对象echo $test->username="张三";echo $test->job();
<?phpclass User{public $username;//类的属性 公共属性protected $email="zs@163.com"; //受保护的属性private $age;function name(){echo "I'm name is :".$this->username;}function email(){echo $this->username."的邮箱地址是:".$this->email;}}$test=new User;//实例化对象echo $test->username="张三";echo "<br>";echo $test->name();echo "<br>";echo $test->email();//从email方法中可以使用$email属性
<?phpclass User{public $username;//类的属性 公共属性protected $email; //受保护的属性private $age;//私有属性//利用构造函数可以访问,受保护和私有化成员function __construct($username, $email, $age){$this->username = $username;$this->email = $email;$this->age = $age;}function name(){return "I'm name is :".$this->username;}function email(){return $this->username."的邮箱地址是:".$this->email."年龄是:".$this->age;}}$test=new User("张三","zs@163.com",28);echo $test->name();echo "<br>";echo $test->email();?>
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号