批改状态:合格
老师批语:面向对象的知识点非常多,而且也比较抽象, 多看看手册
// 对象的模板: 类。下面为示例的代码,非真代码。// class Demo// {// // 属性: 数据复用// $a;// $b;// $c;// // 方法: 代码复用// function get($a,$b, $c) {// return $a + $b +$c;// }// }
// 1. 类的声明class Goods{// 类成员:// 1. 类属性// 2. 类方法public $a = 'abc';// 类的实例构造方法/器public function __construct(){}}// 2. 类的实例化: 就是创建对象的对象// 类中的构造方法必须用: new 来调用$goods = new Goods();// var_dump($goods);// var_dump($goods instanceof Goods);// 实例与对象,在不引起误会的场景下,可以通用// 动态类, 将类名称放在一个变量中$className = 'Goods';$obj = new $className();echo get_class($obj);
<?phpclass User{// 类的属性// 公共成员: 在类的内部和外部都可以访问,都可见public $name = '胡八一';public $options = [1,2,3];// heredoc,nowdoc都支持// 静态属性:如果这个属性仅使用一次,可以不创建类实例,而直接通过类来调用public static $nation = 'CHINA';// 抽象属性,没被初始化的属性, 默认赋NULLpublic $salary;}// 客户端代码// 类的实例化$user = new User;// 类中的成员属性的访问echo $user->name;// print_r($user->options[1]);// 访问静态成员使用双冒号: 范围解析符// echo User::$nation; 静态成员可以直接访问,不用实例化。var_dump(is_null($user->salary));//判断抽象属性是否为null。
<?phpclass User{// public: 访问限定符,用来声明类成员的作用域// 类属性public $name = '胡八一';public $age = 40;public static $nation = 'CHINA';// 类方法public function write1(){// 访问类属性// 1. 实例化$user = new User;// 2 访问类成员return "姓名:{$user->name},年龄: {$user->age}";}// 类方法public function write2(){// 声明一个变量, 声明在方法中的变量,不是属性,是私有变量/局部变量$salary = 8000;// 访问类属性// 1. 实例化// 第一个问题: 类的名称被限定,使用self可以引用当前的类名称// $user = new self;// $this会自动创建, $this是只读的,不能被更新// $this = new self;// 2 访问类成员// 第二个问题: 类的实例,类中并不关心,它也只需要一个实例的引用就可以了// 引用当前类实例: $thisreturn "姓名:{$this->name},年龄: {$this->age}, 工资: {$salary}";// self: 类引用// $this: 类实例引用}// 类方法中访问(类)外部成员: 变量和函数public function write3(){// 调用外部函数$str = hello();// 外部变量return $str . $GLOBALS['username'];}// 实际项目中,外部成员应该通过方法参数注入到方法,而不是在方法中直接调用// 依赖注入public function write4(Closure $hello, string $username){return $hello() . $username;}// 静态方法: 不依赖类实例,可以用类直接调用public static function write5(){// 静态成员属于类,不属性于类实例,所以不能使用类实例的引用$this// return "姓名:{$this->name},年龄: {$this->age}";return "国籍: " . self::$nation;}// 普通方法中可以访问静态成员, 而反之不行public function write6(){return "国籍: " . self::$nation;}}// 客户端代码$user = new User;echo $user->write1();echo '<hr>';echo $user->write2();// 外部函数function hello(){return 'Hello ';}// 匿名函数$hello = function (){return 'Hello ';};// 外部变量$username = '大金牙';echo '<hr>';echo $user->write3();echo '<hr>';echo $user->write4($hello, $username);echo '<hr>';echo $user->write5();echo '<hr>';echo $user->write6();
<?phpclass User{// public: 公共成员, 类外部和内部以及子类中均可使用public $name = '胡八一';// protected: 受保护成员, 除了外部不可见,内部和子类中均可见protected $salary = 7788;// private: 私有成员,仅限于本类内部使用,外部或子类均不可见private $age = 30;// 类内部public function write(){return "姓名: {$this->name},年龄 : {$this->age}, 工资: {$this->salary}";}}// 子类class Rob extends User{public function write(){// return "姓名: {$this->name},年龄 : {$this->age}";// 子类中: 只有public 和 protected可见return "姓名: {$this->name}, 工资: {$this->salary}";}}// 类外部$user = new User;// echo $user->name;// echo $user->age;// echo $user->salary;echo '<hr>';echo $user->write();echo '<hr>';// 子类中的访问echo (new Rob)->write();
<?php// final: 类前加final禁止类扩展final class User{// 属性protected $name = '胡八一';// 方法// final: 方法前加:禁止重写public function write(){return "姓名: {$this->name}";}}// 扩展类/子类class Rob extends User{// 1. 扩展// 属性扩展protected $age = 90;// 方法扩展// public function write()// {// return parent::write() . ", 年龄: {$this->age}";// }// 2. 重写// 属性重写protected $name = '王胖子';// 方法重写public function write(){return "{$this->name} : {$this->age} 岁";}}// 调用$rob = new Rob;echo $rob->write();
<?phpclass Human{public $height = '180cm';public $weight = '80kg';protected $brain = 'middle';protected $heart = 'fraigle';private $motion = 'good';private $power = 'infinite';public function makevalue(){return "Although one man is only {$this->height} high, but his power is {$this->heart}!";}}$tom = new Human;echo $tom->makevalue();echo "<br><hr>".$tom->private."<br><hr>";//尝试了下protected变量外外部访问会报错。private不会报错,但不会显示认识东西。class Woman extends Human{public $height = '170cm';public $weight = '55kg';private $motion = 'lovely';public static $habit = 'excellent';public function nicestyle(){echo '<br><hr>';return "When you meet a {$this->motion} woman about {$this->height} high, you should know she is very {$this::$habit}!";} //这个地方注意下static静态变量的引用。}$mary = new Woman;echo $mary->nicestyle();
结果如图:
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号