お兄さん、とても混乱しているようです

WBOY
リリース: 2016-06-13 13:23:19
オリジナル
731 人が閲覧しました

とても混乱しています

PHP コード
<!--

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 コード
クラスcA
{
    /*** 直接デフォルト値を使用するためのテスト プロパティ
     * 直接のデフォルト値を使用してプロパティをテストする*/
    protected static $item = 'Foo';
    
    /*** 間接的なデフォルト値を使用するためのテスト プロパティ
     * 間接的なデフォルト値を使用してプロパティをテストする*/
    protected static $other = 'cA';
    
    パブリック静的関数メソッド()
    {
        __METHOD__ . ' ' . __CLASS__ . self::$item."rn";
        __METHOD__ . ' ' . __CLASS__ . self::$other."rn";
    }
    
    パブリック静的関数 setOther($val)
    {
        self::$other = $val; // このスコープに値を設定します。
    }
}

クラス cB は cA を拡張します
{
    /*** 再定義されたデフォルト値を使用してプロパティをテストします
     * デフォルト値のテスト属性を再定義しました*/
    protected static $item = 'Bar';
    
    パブリック静的関数 setOther($val)
    {
        self::$other = $val;
    }
}

クラス cC は cA を拡張します
{
    /*** 再定義されたデフォルト値を使用してプロパティをテストします
     * デフォルト値のテスト属性を再定義しました*/
    protected static $item = 'Tango';
    
    パブリック静的関数メソッド()
    {
        __METHOD__ . ' ' . __CLASS__ . self::$item."rn";
        __METHOD__ . ' ' . __CLASS__ . self::$other."rn";
    }
    
    /*** setOther() メソッドの再宣言を削除し、楽しみのために cA を 'self::' とともに使用します。*/
}

クラス cD は cA を拡張します
{
    /*** 再定義されたデフォルト値を使用してプロパティをテストします
     * デフォルト値のテスト属性を再定義しました*/
    protected static $item = 'フォックストロット';
    
    /*** この問題を解決するために、すべてのメソッドの再宣言を削除します。
     * この問題を解決するために、すべての再宣言方法を放棄します。*/
}

cB::setOther('cB'); // cB::method() です!
cB::method(); // cA::method() です!
cC::setOther('cC'); // cA::method() です!
cC::method(); // cC::method() です!
cD::setOther('cD'); // cA::method() です!
cD::method(); // cA::method() です!
 <div class="clear"></div>
ログイン後にコピー
関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!