PHP学习——面向对象


构造函数

  • class定义类,类的名称是Person,首字母大写,其他字母小写
  • 三个属性分别是$name,$sex,$age
  • 类的方法有showinfo()
  • 使用new关键字调用构造函数,创建对象
  • 类里面有一个隐藏的构造函数,构造方法
  • 构造函数定义方法--
<?php
class Person{
}

class Student{
    public $name,$sex,$age;
    function showinf(){  //定义一个方法showinfo()
        echo $this->name."<br />";
        echo $this->sex."<br />";
        echo $this->age."<br />";
    }
    function __construct()  //构造函数,
    {
        echo "I AM BAD!<br />";
    }
}
$st1 = new Student();  //调用构造函数,构造方法,创建了对象$st1
$st1->name = 'Bad';  //使用对象->方法,使用对象->方法
$st1->sex = 'man';
$st1->age = '21';
$st1->showinfo();
?>
  • 类包含有且只有一个构造方法
  • 如果没有定义构造方法,则存在一个隐含的构造方法
  • 如果定义了构造方法,则不存在隐含的构造方法

例:

<?php
class Student{
    public $name,$sex,$age;
    function showinfo(){
        echo $this->name."<br />";
        echo $this->sex."<br />";
        echo $this->age."<br />";
    }
    function __construct($n,$s,$a)  //形式参数,构造函数用于给属性赋初始值
    {
        $this->name = $n;
        $this->age = $a;
        $this->sex = $s;
    }
}

$st1 = new Student('Bad','man','21');  //调用构造方法
$st1->showinfo();
?>

析构函数

  • 对象不用了会自动销毁
  • 程序结束,对象自动销毁
  • unset销毁对象
  • 赋值unll,销毁对象
<?php
class Bad{
    function __destruct()
    {
        echo "I AM BAD BOY!<br />";
    }
    function __construct()
    {
        echo "Hello!<br />";
    }
}

$a = new Bad();
echo "bad<br />";
$a2 = new Bad();
echo "boy<br />";
?>
运行结果:
			Hello!
			bad
			Hello!
			boy
			I AM BAD BOY!
			I AM BAD BOY!

例:

<?php
class Cft{
    public $long,$wide,$high;
    private function volume(){  //这里的private如果不定义,则可以在外面调用
        return $this->long * $this->wide * $this->high;
    }
    function showinfo(){
        echo $this->volume()."<br />";
    }
    function __construct($l,$w,$h)
    {
        $this->long = $l;
        $this->wide = $w;
        $this->high = $h;
    }
    function __destruct()
    {
        echo "Bad<br />";
    }
}

$a1 = new Cft(3,4,5);
$a1->showinfo();
?>

继承

  • 子类从父类中得到属性和方法的过程
  • 父类中使用public定义的属性、方法是可以继承给子类的
    • 子类继承得到后,访问的权限还是public
  • 在父类中使用protected定义的受保护的属性、方法,是可以继承给子类的
    • 子类继承得到后,访问的权限还是protected
  • 私有属性方法不能继承,只能在类的里面访问
  • 子类中定义了与父类相同名的方法,父类中的方法会被替换
  • 单继承,一个类只能继承一个父类,不能有多个父类
<?php

class Person{
    public $name;  //公有的属性方法,可以继承,可以在类的外面访问
    private $sex;  //受保护的属性方法,只能在类的里面访问,不可以继承
    protected $age;  //受保护的属性方法,只能在类的里面访问,可以继承
    function fun1(){
        echo "fun1()<br />";
    }
    private function fun2(){
        echo "fun2()<br />";
    }
    function showinfo(){  //不加修饰,默认的public权限
        echo $this->name."<br />";
    }
}
class Student extends Person{  //Student是子类,继承Person
    //子类中即使不显示定义的属性方法,也含有属性和方法(从父类中继承)
}

$st1 = new Student();
$st1-> name = 25;
$st1->fun1();
$st1->showinfo();
  • 子类可以调用父类的方法,但是不能调用私有的方法
<?php

class Person{
    function fun1(){
        echo "fun1()<br />";
    }
    function __construct()
    {
        echo "bad";
    }
}
class Student extends Person{
    function showme(){
        parent::fun1();  //子类调用父类方法,调用父类的fun1()
        $this->fun1();  //调用下面的fun1()
    }
    function __construct()
    {
        echo "Boy";
    }
    function fun1(){
        echo "Student()<br />";
    }
}

$st1 = new Student();
?>

私有属性的访问

  • __set()方法,用于给私有属性赋值
<?php

class Student{
    private $sex,$age;
    function __set($s, $a)
    {
        echo "bad<br />";
        $this->$s = $a;
    }
    function __get($Stu){
        return $this->$Stu;
    }
}

$Stu = new Student();  //构造方法创建对象
$Stu->sex = 'male';  //自动调用__set($s,$a)方法,$s得到sex,$a得到'male'
echo $Stu->sex;  //得到私有属性值时,自动访问方法__get($Stu)方法
  • 年龄大于100输出The vlaue input is error!,小于100的原样输出。sex不是Male或Famale的终止程序,并提示信息
<?php
# header("Content-Type:text/html;charset=utf-8");
# @Time : 2020/5/3
# @Software: PhpStorm
# @Author: Bad
# @DESC:

class Age extends Exception{  //Exception异常类
    private $sex, $age;
    function __set($property,$value){
        if ($property == 'age'){
            if($value > 100)
                $this->$property = 'The vlaue input is error!';
            else
                $this->$property = $value;
        }
        if ($property == 'sex'){
            if($value == 'Male'  || $value == 'Famale')
                $this->$property = $value;
            else{  //抛出异常
                throw new Exception('The sex must be Male or Famale!');
            }
        }
    }
    function showinfo(){
        echo $this->age."<br />";
        echo $this->sex."<br />";
    }
}
# 直接修改sex值
$Stu = new Age();
$Stu->age = 105;
$Stu->sex = 'Male';
$Stu->showinfo();
# 终止程序,提示信息
$Stu = new Age();
$Stu->age = 55;
$Stu->sex = 'aaa';
$Stu->showinfo();

接口

  • 定义类实现接口,一个类可以实现多个接口
    • 必须实现接口中的全部方法
    • 重复的方法可以只实现一个
  • 一个类继承一个类,同时实现一个或多个接口
<?php
interface Interface1{
    function Login($user,$post,$sex);
    function Showinfo();
    function query($id);
}
interface Interface2{
    function login($user,$post,$sex);
    function insertuser($user,$post,$age,$pwd);
}

class Student implements Interface1,Interface2{
    public Login($user,$post,$pwd);
    function Login($user, $post, $sex){
        $this->user = $user;
        $this->post = $post;
        $this->pwd = $pwd;
    }
    function showinfo(){
    }
    function query($id){
    }
    function insertuser($user, $post, $age, $pwd){
    }
}
以上内容均属原创,如有不详或错误,敬请指出。
本文作者: 坏坏

猜你喜欢

转载自blog.csdn.net/qq_45668124/article/details/105905807