`

PHP学习(15)面向对象开发学习(5)

    博客分类:
  • php
PHP 
阅读更多


什么函数都没加时
<?php

class MyPc{
  public $name='my computer';
  function power(){//final当定义类的时候该类将不能被继承
   echo $this->name.",the computer is opening...";
}

}

class My extends MyPc{//extends重载

  function power(){
   echo "********";//继承时覆盖同名之前类
   echo MyPc::power()."********";//继承时不会覆盖同名之前类,加到之前类后输出
}
}

$p=new My();

$p->power();


?>



final(锁定)
<?php

final class MyPc{//final(锁定)当定义类的时候该类将不能被重载
public $name='my computer';
final function power(){//final(锁定)当定义方法的时候该方法将不能被重载
  echo $this->name.",the computer is opening...";
}

}

$p=new MyPc();

$p->power();


?>



static静态属性,self访问静态属性
<?php

class MyPc{
static $name='my computer';//无法被访问
static function power(){
  //静态属性需要用self来访问
  echo self::$name.",the computer is opening...";
}

}


//$p=new MyPc();

//$p->power();



echo MyPc::$name="你的diannao";//可以访问,静态属性已经在内存之中,不用实例化,可以修改
echo MyPc::power();



?>

const只能修饰类当中的成员属性!建议大写常量常量,不使用$符号。
<?php

final class MyPc{
const NAME='my computer';
static function power(){
  //静态属性需要用self来访问
  echo self::NAME.",the computer is opening...";
}

}


$p=new MyPc();

$p->power();



?>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics