zoukankan      html  css  js  c++  java
  • 跟着百度学PHP[4]OOP面对对象编程-11-Final关键字

    Final的作用就是不允许儿子继承夫类,也就是说不能够对父类在进行调用,否则将会出错。 

    目录---------------------------------------------------------------------------------------------

    00X1 对父类的属性或方法进行重写(直接在子类当中写一个与夫类相同的属性或者方法)案例一

    00x2 不允许子类重写属性或者方法(在属性或方法处加final)案例二

    00x3 不允许子类重写父类(直接在类那里加final)案例三

    ==》总的来说就是在属性或方法出加final就是不允许对属性或方法进行重写。同理在类出加final加即不允许对父类进重写。

    -----------------------------------------------------------------------------------------------

    00x1 子承父的重写(案例一)

    <?php 
    class Fulei
    {
        function test1(){
            echo "This is test1";
        }
        function test2(){
            echo "This is test2";
        }
    }
    class Zilei extends Fulei{
        function test1($name){ #对test1进行重写,这里不管参数是什么,只要方法名与父类的一致就会被重写
            echo "励志成为网络安全大牛!".$name."
    ";
        }
    }
        $object=new Zilei();
        $object->test1("张三");
     ?>

    输出如下图所示

    如图所示,成功的将其重写了。

    00x2 不允许子类重写属性或者方法(案例二)

    <?php 
    class human
    {
        final public function test(){     #注意我在此行加了final 不允许子类重写该方法。
                echo "this is 1";
              }
              public function test2(){
                echo "this is 2"; 
              }
    }
    
    class Studen extends human
    {
        public function hello()
        {
            echo "hello world";
        }
        public function test()   #对父类进行重写!
        {
            echo "我要做一个大牛!";
        }
    }
        $one=new Studen;
        $one->test();
    
     ?>

    就会爆出如下错误:

    ( ! ) Fatal error: Cannot override final method human::test() in D:wampwww1.php on line 22

     00x3 不允许子类重写属性或者方法(案例三)

    <?php 
    final class human#注意我此行加了final 不允许子类重写该类。
    {
              public function test(){     
                echo "this is 1";
              }
              public function test2(){
                echo "this is 2"; 
              }
    }
    class Studen extends human
    {
        public function hello()
        {
            echo "hello world";
        }
        public function test()   #对父类进行重写!
        {
            echo "我要做一个大牛!";
        }
    }
        $one=new Studen;
        $one->test();
     ?>

    爆出如下错误:

    ( ! ) Fatal error: Class Studen may not inherit from final class (human) in D:wampwww1.php on line 22

    THE END


  • 相关阅读:
    Javascript构造函数的继承
    什么数据库能抗住《王者荣耀》的1亿DAU?
    支持微信支付亿级请求的TBase数据库大揭秘
    我在MySQL的那些年(一)
    谁是银行核心数据库的破局者?
    X侦探所事件薄 | 一次内存溢出之谜
    腾讯云数据库新生代产品获国家级认证
    POJ 2594 传递闭包的最小路径覆盖
    POJ 1719 二分图最大匹配(记录路径)
    HDU 1533 KM算法(权值最小的最佳匹配)
  • 原文地址:https://www.cnblogs.com/xishaonian/p/6151307.html
Copyright © 2011-2022 走看看