小弟我觉得好迷糊

WBOY
发布: 2016-06-13 13:23:19
原创
732 人浏览过

我觉得好迷糊啊

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php class cA
{
    /**
     * Test property for using direct default value
     */
    protected static $item = 'Foo';
    
    /**
     * Test property for using indirect default value
     */
    protected static $other = 'cA';
    
    public static function method()
    {
        print self::$item."\r\n"; // It prints 'Foo' on everyway... :(
        print self::$other."\r\n"; // We just think that, this one prints 'cA' only, but... :)
    }
    
    public static function setOther($val)
    {
        self::$other = $val; // Set a value in this scope.
    }
}

class cB extends cA
{
    /**
     * Test property with redefined default value
     */
    protected static $item = 'Bar';
    
    public static function setOther($val)
    {
        self::$other = $val;
    }
}

class cC extends cA
{
    /**
     * Test property with redefined default value
     */
    protected static $item = 'Tango';
    
    public static function method()
    {
        print self::$item."\r\n"; // It prints 'Foo' on everyway... :(
        print self::$other."\r\n"; // We just think that, this one prints 'cA' only, but... :)
    }
    
    /**
     * Now we drop redeclaring the setOther() method, use cA with 'self::' just for fun.
     */
}

class cD extends cA
{
    /**
     * Test property with redefined default value
     */
    protected static $item = 'Foxtrot';
    
    /**
     * Now we drop redeclaring all methods to complete this issue.
     */
}

cB::setOther('cB'); // It's cB::method()!
cB::method(); // It's cA::method()!
cC::setOther('cC'); // It's cA::method()!
cC::method(); // It's cC::method()!
cD::setOther('cD'); // It's cA::method()!
cD::method(); // It's cA::method()!

/**
 * Results: ->
 * Foo
 * cB
 * Tango
 * cC
 * Foo
 * cD
 * 
 * What the hell?! :)
 */

?>
登录后复制



这是覆盖,还是什么啊?为什么输出这样啊,不能理解啊,听乱的啊。

------解决方案--------------------
这叫什么呢?自找麻烦!
为累而累
------解决方案--------------------
不理解就算了,无所谓的事。
------解决方案--------------------
探讨

这叫什么呢?煮饺子找麻烦!
为累而累

------解决方案--------------------
这样可能会清楚些
PHP code
class cA
{
    /**
     * Test property for using direct default value
     * 使用直接默认值测试属性
     */
    protected static $item = 'Foo';
    
    /**
     * Test property for using indirect default value
     * 使用间接默认值测试属性
     */
    protected static $other = 'cA';
    
    public static function method()
    {
        print __METHOD__ . ' ' . __CLASS__ . '::$item=' . self::$item."\r\n";
        print __METHOD__ . ' ' . __CLASS__ . '::$otfer=' . self::$other."\r\n";
    }
    
    public static function setOther($val)
    {
        self::$other = $val; // Set a value in this scope.
    }
}

class cB extends cA
{
    /**
     * Test property with redefined default value
     * 重新定义了默认值测试属性
     */
    protected static $item = 'Bar';
    
    public static function setOther($val)
    {
        self::$other = $val;
    }
}

class cC extends cA
{
    /**
     * Test property with redefined default value
     * 重新定义了默认值测试属性
     */
    protected static $item = 'Tango';
    
    public static function method()
    {
        print __METHOD__ . ' ' . __CLASS__ . '::$item=' . self::$item."\r\n";
        print __METHOD__ . ' ' . __CLASS__ . '::$otfer=' . self::$other."\r\n";
    }
    
    /**
     * Now we drop redeclaring the setOther() method, use cA with 'self::' just for fun.
     */
}

class cD extends cA
{
    /**
     * Test property with redefined default value
     * 重新定义了默认值测试属性
     */
    protected static $item = 'Foxtrot';
    
    /**
     * Now we drop redeclaring all methods to complete this issue.
     * 现在,我们放弃重新声明的所有方法来完成这个问题
     */
}

cB::setOther('cB'); // It's cB::method()!
cB::method(); // It's cA::method()!
cC::setOther('cC'); // It's cA::method()!
cC::method(); // It's cC::method()!
cD::setOther('cD'); // It's cA::method()!
cD::method(); // It's cA::method()!
 <div class="clear">
                 
              
              
        
            </div>
登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!