Blogger Information
Blog 47
fans 0
comment 3
visits 44717
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
类的定义封装、继承,接口实现及魔术方法练习
江流
Original
626 people have browsed it
  • auto.php 文件
  1. <?php
  2. spl_autoload_register(
  3. function($className){
  4. // $className 是new的Son这个名字,这个名字是类名,不是文件名
  5. require $className . '.php';
  6. }
  7. );
  • 抽象类Animal文件animal.php
  1. <?php
  2. abstract class Animal{
  3. public $name;
  4. protected $age;
  5. protected $sound;
  6. protected $count;
  7. protected $color;
  8. abstract public function shout();
  9. public function __get($key){
  10. if($key=='sound'|| $key=='count'){
  11. $resout= $this->$key;
  12. }elseif(isset($this->$key)){
  13. $resout= "您访问的成员不能访问";
  14. }else{
  15. $resout= "您访问的成员不存在。";
  16. }
  17. return $resout;
  18. }
  19. public function __set($key,$value){
  20. if($key=='sound' or $key=='count'){
  21. return $this->$key=$value;
  22. }
  23. echo "您不可以设置$key 这个成员。";
  24. }
  25. //__call 处理没有定义的方法
  26. public function __call($name, $arguments)
  27. {
  28. $count=count($arguments);
  29. $resout="您访问的方法:$name. 传入{$count}个参数,";
  30. if($count>0){
  31. foreach($arguments as $arg){
  32. $resout .=$arg .",";
  33. }
  34. }
  35. $resout .="该方法不存在。";
  36. return $resout;
  37. }
  38. //访问未定义的静态方法,触发__callStatic方法
  39. public static function __callStatic($name, $arguments)
  40. {
  41. return "方法$name 不存在。";
  42. }
  43. }
  • dog.php文件,定义了Dog类
  1. <?php
  2. require_once "auto.php";
  3. class Dog extends Animal{
  4. public function shout()
  5. {
  6. $res="我的名字叫:".$this->name.",我{$this->age}岁了,";
  7. for($i=0;$i<$this->count;$i++){
  8. $res.=$this->sound." ";
  9. }
  10. return $res;
  11. }
  12. public function __construct($name,$sound="",$age=3)
  13. {
  14. $this->name=$name;
  15. $this->age=$age;
  16. $this->sound=$sound;
  17. }
  18. }
  • 接口文件 fly.php
  1. <?php
  2. interface Fly{
  3. public function canFly();
  4. }
  • 实现接口的类文件:EspecialDog.php
  1. <?php
  2. require_once "auto.php";
  3. //继承类,实现接口
  4. class EspecialDog extends Dog implements Fly{
  5. public function canFly()
  6. {
  7. return "我是$this->name,我会飞";
  8. }
  9. }
  • 练习
    ```php
    <?php
    require_once “auto.php”;

$d=new Dog(‘阿黄’,”汪”,3);

echo $d->name;
echo “<HR>“;
echo $d->sound;
echo “<HR>“;
echo $d->age;
echo “<HR>“;
//设置叫的次数
$d->count=5;
echo $d->shout();
echo “<HR>“;
echo $d->aa;
echo “<HR>“;
//设置一个不存在的成员,给出提示“您不可以设置a 这个成员”
$d->a=”aaa”;
echo “<HR>“;
echo $d->f(100,33);
echo “<hr>“;

$edog=new EspecialDog(“哮天犬”);
echo $edog->canFly();
```

  • 结果

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:作业完成后可以预览一下效果
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