面向对象编程(object-oriented programming) [ˈɑːbdʒekt ˈɔːrientɪd ˈproʊɡræmɪŋ]
对象是一个由信息及对信息进行处理的描述所组成的整体,是对现实世界的抽象。
在现实世界里我们所面对的事情都是对象,如猫、狗、鸟等。
比如 Animal(动物) 是一个抽象类,我们可以具体到一只狗跟一只羊,而狗跟羊就是具体的对象,他们有颜色属性,可以写,可以跑等行为状态。
父类:拥有部分相同的属性和方法
继承的好处:
子类
:
创建如下文件:
<?php
// 成员属性前要有访问修饰符 public private protected
// public 默认的, 关键词定义类内、类外、子类都可见
// protected 关键词定义类内、子类可见,类外不可见
// private 关键词定义类内可见, 子类、类外不可见
class Cat
{
public $weight;
public $species;
public $color;
// protected属性只能由本类或者子类访问
protected $orprotect;
// private属性只能由本类访问
private $caneat;
// 成员方法
public function sleep()
{
echo "$this->species is sleeping <br>";
}
public function hunt()
{
echo "$this->species is hunting<br>";
}
public function caneat()
{
echo "$this->species$this->caneat";
}
public function privateeat()
{
//返回私有属性
return $this->caneat;
}
// 构造函数 构造器 类每实例化一次 构造函数自动被调用
// 类成员之间的互相访问 $this 本对象
// 1.初始化类成员 让类/对象的状态稳定下来
// 2.给对象的属性进行初始化赋值
// 3.给私有或者受保护的成员属性赋值
public function __construct($weight, $species, $color, $orprotect, $caneat)
{
$this->weight = $weight;
$this->species = $species;
$this->color = $color;
$this->orprotect = $orprotect;
$this->caneat = $caneat;
}
}
// new关键字完成类的实例化 得到对象
//$cat1 = new Cat('2kg', '橘猫', '橘色', '不是', '能吃');
//var_dump($cat->caneat);//Cannot access private property Animal::$caneat
// $cat1->caneat();//橘猫能吃
//var_dump($cat1->privateeat());//string '能吃' (length=6)
// object(Cat)[1]
// public 'weight' => string '2kg' (length=3)
// public 'species' => string '橘猫' (length=6)
// public 'color' => string '橘色' (length=6)
// protected 'orprotect' => string '不是' (length=6)
// private 'caneat' => string '能吃' (length=6)
<?php
class Bird
{
public $name;
public $color;
public $taste;
protected $oreat;
private $orprotect;
public function eat1()
{
return $this->oreat;
}
public function ordefend()
{
echo $this->orprotect;
}
public function sparrowinfo()
{
echo $this->name . "的颜色是" . $this->color . "。<br>" . "味道" . $this->taste . "<br>";
}
public function __construct($name, $color, $taste, $oreat, $orprotect)
{
$this->name = $name;
$this->color = $color;
$this->taste = $taste;
$this->oreat = $oreat;
$this->orprotect = $orprotect;
}
}
// $sparrow = new Bird('麻雀', '黑棕色', '美味', '能吃', '不是');
// $sparrow->sparrowinfo();
// echo $sparrow->eat1();
<?php
class Dog
{
public $name;
public $color;
public $taste;
protected $oreat;
private $orprotect;
public function eat1()
{
return $this->oreat;
}
public function ordefend()
{
echo $this->orprotect;
}
public function sparrowinfo()
{
echo $this->name . "的颜色是" . $this->color . "。<br>" . "味道" . $this->taste . "<br>";
}
public function __construct($name, $color, $taste, $oreat, $orprotect)
{
$this->name = $name;
$this->color = $color;
$this->taste = $taste;
$this->oreat = $oreat;
$this->orprotect = $orprotect;
}
}
<?php
class Product{
public $type;
public $color;
protected $weight;
private $price;
public function weight(){
return $this->weight;
}
public function price(){
echo $this->price;
}
public function __construct($type,$color,$weight,$price)
{
$this->type = $type;
$this->color = $color;
$this->weight = $weight;
$this->price = $price;
}
}
<?php
class Sonproduct extends Product
{
}
<?php
//类的自动加载器前提是类名称跟类文件名称保持一致(psr-4规范)
spl_autoload_register(function ($classname) {
$file = __DIR__ . DIRECTORY_SEPARATOR . $classname . '.php';
echo $classname . ".php" . "<br>";
if(!(is_file($file) && file_exists($file))){
throw new \Exception("文件不合法或者不存在");
}
require $file;
});
相对于第三方框架、库,我们就是客户端代码文件,我们直接实例化对象
<?php
//相对于第三方框架、库,我们就是客户端代码文件
require 'autoload.php';
//直接实例化对象
$cat1 = new Cat('2kg', '橘猫', '橘色', '不是', '能吃');
$sparrow = new Bird('麻雀', '黑棕色', '美味', '能吃', '不是');
$husky = new Dog('哈士奇', '黑白', '未知', '应该能', '不是');
$computer = new Product('台式电脑','black','5kg','¥6666');
$laptop = new Sonproduct('笔记本','炫酷黑','1.5kg','¥8888');
var_dump($cat1);
var_dump($sparrow);
var_dump($husky);
var_dump($computer);
var_dump($laptop);
echo $laptop->price();
打印结果如下:
相关推荐
© 2020 asciim码
人生就是一场修行