Blogger Information
Blog 14
fans 0
comment 0
visits 8790
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
public protected private 的应用
于星辉
Original
551 people have browsed it

oop基础:请举例实例演绎以下难点

  1. 类(对象抽象化的结果)与对象 (类实例化结果)
    ```php
    <?php
    /**
    • 1.变量是实现数据的利用,函数是实现代码块的利用
    • 2.类是具有相同属性和方法的对象的集合
    • 3.对象是复合数据类型
    • 4.oop(面向对象的程序设计中)的三大特性:封装性、继承性、多态
    • 封装是:隐藏对象的属性或方法(实现细节),目的是为了控制在程序中属性的读或修改的访问级别,
    • 使用者只城要通过外部接口以特定的权限来使用类成员。
      */
      class Player{
      //成员属性前一定要有访问修饰符:public protected private
      public $name;
      public $height;
      public $team;
      // protected 只能被本类和子类访问
      protected $playNum = 23;
      // private 只能被本类访问
      private $weight;
  1. //如何给 protected private 属性赋值
  2. // __construct()魔术方法 也叫构造函数、构造器:类实例化一次就被自动调用一次
  3. public function __construct($name,$height,$team,$playNum,$weight){
  4. /**
  5. * 1.初始化类成员,让类、实例化的状态稳定下来
  6. * 2、给对象属性进行初始化赋值
  7. * 3、可以给私有成员,爱保护的成员初始化赋值
  8. */
  9. $this->name = $name;
  10. $this->height = $height;
  11. $this->team = $team;
  12. $this->playNum = $playNum;
  13. $this->weight = $weight;
  14. }
  15. public function jog(){
  16. //$this 特殊的对象引用:代表对象
  17. return "$this->name is jogging ,whose playNum is $this->playNum<br>";

}

public function shoot(){
return “$this->name is shooting ,weighting $this->weight<br>“;
}
}
$jordan = new Player(‘Jordan’,’203cm’,’Bulk’,6,’80kg’);
//echo $jordan->weight.’<br>‘;
echo $jordan->shoot();
?>

  1. 2. 构造方法
  2. ```php
  3. public function __construct($name,$price)
  4. {
  5. $this->name = $name;
  6. $this->price = $price;
  7. }
  1. 对象成员之间的内部访问 $this

    1. public function show(){
    2. return "{$this->name}的单价为{$this->price}元";
    3. }
  2. private仅限本类中使用 protected本类中,子类中使用
    class User{
    public static $name =’胡歌’;
    protected $_config =[

    1. 'auth_on' =>'true',
    2. 'auth_type'=> 1

    ];

  1. public static $nation = 'China';
  2. private $salary;
  3. static $count = 0;
  4. public function __construct($name,$salary){
  5. //静态成员与类的实例无关,不能用$this访问,用self::类的引用
  6. // 访问静态成员
  7. self::$name = $name;
  8. self::$count++;
  9. $this->salary =$salary;
  10. }
  1. 类的自动加载 spl_autoload_register
    <?php
    /**

    • 类的自动加载:类名称与类文件名称保持一致
      */

    spl_autoload_register(function($className)){
    require $className.’.php’;
    }

  2. 静态成员的访问 类的引用self::
    public function __construct($name,$salary){
    1. //静态成员与类的实例无关,不能用$this访问,用self::类的引用
    2. // 访问静态成员
    3. self::$name = $name;
    4. self::$count++;
    5. $this->salary =$salary;
    }
  3. 类的继承 扩展 父类方法(魔术方法,普通方法)的重写 parent:: 调用父类成员
    class Son extends Product
    {
    //属性扩展
    private $num;
    //重写父类构造器
    public function __construct($name,$price,$num){
    1. //parent关键字,调用父类中的成员方法
    2. parent::__construct($name,$price);
    3. $this->num = $num;
    }
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