oop.2

1.封装性:就是把对象的属性和方法结合成独立的相同对象,并且隐藏对象的内部细节。

class person{

   private $name;

  private function say(){

  echo ...;

  }

}

$p1=new person();

$p1->name="zhangsan";   //Cannot access private property

$p1->say();   //Call to private method Person::say()

 

如果想在类的外面给私有属性赋值和读取。也就是给类的外部提供一些接口,构造函数就是一种赋值形式,但知识在创建对象的时候赋值,这时候就要用到_set() _get()

 _get()方法来获得私有属性

 private function _get($property_name){

   if(issue($this->property_name)){reture ($this->property_name);}

  else{reture(null);}

}

 

_set()方法来设置私有变量

 private function _set($property_name,$value){

 $this->property=$value;

}

 

isset(),_unset()... 是我们添加到对象里面需要时自动调出,来完成对象外部对私有属性的操作

 

2.继承性

表达方式:

class person{...}

class student extends person{...}   一个子类只能继承父类

如果在子类想调用父类被覆盖的方法有两种方法:

1.使用父类的“类名::“来调用

2.使用”parent::“来调用

 

3.多态性

多态就是把子类对象赋值给父类引用,然后调用父类的方法气执行子类覆盖父类的方法。所以多态一定有父类对象和子类对象。

eg 做一个形状的接口或者抽象类作为父类,里面有两个抽象的方法,一个求周长,一个求面积。

<?

 interface shape{

function area();

function perimeter();

}

?>

 

class rect inplements shape{

 private $width;

 private $height;

 function _constract($width,$height){

 $this->width=$width;

 $this->height=$height;

}

 function area(){ reture ($this->weight*$this->height);}

 function perineter(){...}

 

class Circular inplements shape(){

...}

$shape=new rect(5,10)  //把子类矩形对象给形状的一个引用

echo $shape->area();

echo $shape->primeter(); 

$shape=new Circular(10) //把子类圆形对象给形状的一个引用

echo $shape->area();

echo $shape->primeter();

 

posted on 2016-08-01 22:23  yidao123  阅读(126)  评论(0编辑  收藏  举报

导航