8 OOP三大特征
作者:互联网
l 基本介绍
oop编程的三大特性是:封装,继承,多态,在php面向对象编程中,多态提的并不多,以为php本身就是多态。
l 抽象
在面向对象编程中,将一类事物共有的属性和行我提取出来,形成一个模板(类),这种解决问题的方法就是抽象。
class Account{
public $accountNo;
private $pwd;
private $balance;
public function __construct($accountNo,$pwd,$balance=0.0){
$this->accountNo = $accountNo;
$this->pwd = $pwd;
$this->balance = $balance;
}
public function saveMoney($pwd,$amount){
if($this->pwd == $pwd && $amount > 0){
$this->balance += $amount;
echo "存储成功,余额为:".$this->balance;
}
}
public function withdraw($pwd,$amount){
if($this->pwd == $pwd && $amount > 0 && $amount < $this->balance){
$this->balance -= $amount;
echo "取出成功,余额为:".$this->balance;
}else{
echo "密码或数值有误";
}
}
public function query($pwd){
if($this->pwd == $pwd){
echo "余额为:".$this->balance;
}
}
}
$account = new Account('10001','111',5000);
$account->query('111');
$account->saveMoney('111',1000);
$account->withdraw('111',500);
1.1 1.封装
隔离性:
被封装后的对象,其外部对象是无法直接访问对象的内部实现细节,内部实现细节的的改动不会影响到外部对象的访问原则(即:对象内部修改后,在公开暴露指定的访问路径不变的情况下,外部访问它的对象是无需修改的),这是隔离性的体现,同时也是实现高内聚,低耦合的最根本的思想之一;
可复用性:
被封装后的对象可以被外部多个对象访问,而无需为每个外部对象去指定不同的服务对象;减少代码的冗余。
可读性:
合理设置被封装的对象名称,可增强代码的可读性,通过方法名或者类名即可了解其作用。
强调:
上面说的访问控制访问,对属性和成员方法都起作用。
1.2 如何操作protected和private
l 分别定义各种方法
class Student{
public $name;
protected $grade;
private $rank;
public function __construct($name,$grade,$rank){
$this->name = $name;
$this->grade = $grade;
$this->rank = $rank;
}
public function setGrade($grade){
if($grade > 0){
echo "modified succeessfully!<br>";
$this->grade = $grade;
}
}
public function getGrade(){
echo "grade is :".$this->grade."<br>";
}
public function setRank($rank){
if($rank > 0){
echo "modified succeessfully!<br>";
$this->rank = $rank;
}
}
public function getRank(){
echo "the rank is :".$this->rank."<br>";
}
}
$student = new Student('lh',700,1);
$student->getGrade();
$student->setGrade(500);
$student->getGrade();
l 将各种方法放在一个方法中
class Student{
public $name;
protected $grade;
private $rank;
public function __construct($name,$grade,$rank){
$this->name = $name;
$this->grade = $grade;
$this->rank = $rank;
}
public function setGrade($grade){
if($grade > 0){
echo "modified succeessfully!<br>";
$this->grade = $grade;
}
}
public function getGrade(){
echo "grade is :".$this->grade."<br>";
}
public function setRank($rank){
if($rank > 0){
echo "modified succeessfully!<br>";
$this->rank = $rank;
}
}
public function getRank(){
echo "the rank is :".$this->rank."<br>";
}
public function toolSet($grade,$rank){
$this->setGrade($grade);
$this->setRank($rank);
}
}
$student = new Student('lh',700,1);
$student->getGrade();
$student->toolSet(500,200);
$student->getGrade();
$student->getRank();
1.3 继承
可以把多个类中相同的部分写在一个父类中,就实现了代码复用,减少赘余代码
class Student{
public $name;
protected $grade;
public function __construct($name){
$this->name = $name;
}
public function getGrade(){
echo "grade is :".$this->grade."<br>";
}
}
class Pupil extends Student{
public function setGrade($grade){
if($grade > 0){
echo "The pupil is testing.<br>";
$this->grade = $grade;
}
}
}
class Graduate extends Student{
public function setGrade($grade){
if($grade > 0){
echo "The graduate is testing.<br>";
$this->grade = $grade;
}
}
}
$pupil = new Pupil("小学生");
$pupil->setGrade(80);
$pupil->getGrade();
$graduate = new Graduate("大学生");
$graduate->setGrade(90);
$graduate->getGrade();
1.4 重载
php中的重载和其它的不同它借助__call魔术方法来达到相似的效果。
class Overload{
private function getSum($sum1,$sum2){
return $sum1+$sum2;
}
private function getMax($sum1,$sum2,$sum3){
return max($sum1,$sum2,$sum3);
}
public function __call($method_name,$parameter){
if($method_name == 'getValue'){
if(count($parameter)==2){
echo $this->getSum($parameter[0],$parameter[1]);
}else if(count($parameter)==3){
echo $this->getMax($parameter[0],$parameter[1],$parameter[2]);
}
}
else{
echo "There is no such method";
}
}
}
$overload = new Overload();
$overload->getValue(1,2);
$overload->getValue(5,44,63);
1.5 重写
1.5.1 方法重写
方法重写就是子类继承父类的方法之后可以修改方法的内容。
class Override{
public $name;
public function speak(){
echo "say someting";
}
}
class Example1 extends Override{
public function speak(){
echo "<br/>sometimes alone sometimes accompanied";
}
}
class Example2 extends Override{
public function speak(){
echo "<br/>stay awesome all the time";
}
}
$override = new Override;
$override->speak();
$example1 = new Example1;
$example1->speak();
$example2 = new Example2;
$example2->speak();
l 一些细节
(1) 子类的方法的参数个数,方法名称,要和父类方法的参数个数,方法名称一样。
//这样是错的
class Override{
public $name;
public function speak(){
echo "say someting";
}
}
class Example1 extends Override{
public function speak($num1){
echo "<br/>sometimes alone sometimes accompanied";
}
}
(2) 如果父类的方法的参数使用了类型约束,还必须和保证数据类型一致。
//这样是错的
class Override{
public $name;
public function speak(array $num1,$num2){
echo "say someting";
}
}
class Example1 extends Override{
public function speak($num1,$num2){
echo "<br/>sometimes alone sometimes accompanied";
}
}
(3) 子类方法不能缩小父类方法的访问权限。
//这样是错的
class Override{
public $name;
public function speak(){
echo "say someting";
}
}
class Example1 extends Override{
private function speak(){
echo "<br/>sometimes alone sometimes accompanied";
}
}
1.5.2 属性重写
class A{
public $name = 'a';
protected $age = '10';
private $salary = '10000';
}
class B extends A{
public $name = 'b';
protected $age = '15';
private $salary = '1000';
}
$b = new B;
echo "<pre>";
var_dump($b);
l 只有public和protected的属性能够进行重写。
1.6 多态
标签:function,name,特征,rank,echo,grade,OOP,public,三大 来源: https://www.cnblogs.com/stonffeliu/p/15968143.html