> 백엔드 개발 > PHP 튜토리얼 > public、protected、private的区别,protectedprivate_PHP教程

public、protected、private的区别,protectedprivate_PHP教程

WBOY
풀어 주다: 2016-07-13 09:53:04
원래의
1059명이 탐색했습니다.

public、protected、private的区别,protectedprivate

通过className::functionNmae()、实例化、parent、self得出结论

className::functionNmae()  需是静态方法(public模式)

parent::functionNmae() 父类需是静态方法(public、protected两种模式)

self::functionNmae() 该类需是静态方法(三种模式)

实例化:实例化后可访问public模式的静态方法或普通方法

 

一、直接访问(通过 className::functionNmae()和实例化):

结论:

            | 可以 通过 className::functionNmae() 访问
public    
            | 可以 通过实例 $obj->functionName() 访问        

            | 不可 通过 className::functionNmae() 访问
protected 
            | 不可 通过实例 $obj->functionName() 访问    

            | 不可 通过 className::functionNmae() 访问
private   
            | 不可 通过实例 $obj->functionName() 访问

<?<span>php
</span><span>/*</span><span>*
 * 
 * @authors HG (hg0728@qq.com)
 * @date    2015-05-26 17:12:02
 * @version 1.0
 </span><span>*/</span>
<span>header</span>("Content-type:text/html;charset=utf-8"<span>);
</span><span>class</span><span> A {
    </span><span>static</span> <span>$name</span>='这是类A的静态变量'<span>;

    </span><span>static</span> <span>public</span> <span>function</span><span> stc_pc(){
        </span><span>echo</span> '这是类A的公共静态方法<br />'<span>;
    }
    </span><span>static</span> <span>function</span><span> stc(){
        </span><span>echo</span> '这是类A的静态方法<br />'<span>;
    }
    </span><span>static</span> <span>protected</span> <span>function</span><span> stc_pd(){
        </span><span>echo</span> '这是类A受保护的静态方法<br />'<span>;
    }
    </span><span>static</span> <span>private</span> <span>function</span><span> stc_pe(){
        </span><span>echo</span> '这是类A私有的静态方法<br />'<span>;
    }
}


A</span>::stc_pc();<span>//</span><span>可访问</span>
<span>
A</span>::stc();<span>//</span><span>可访问</span>
<span>
A</span>::<span>$name</span>;<span>//</span><span>不报错

//A::stc_pd();//不可访问

//A::stc_pe();//不可访问</span>


<span>echo</span> '<br><br>通过实例化访问<br><br>'<span>;


</span><span>$a</span> = <span>new</span><span> A();

</span><span>$a</span>->stc_pc();<span>//</span><span>可访问</span>

<span>$a</span>->stc();<span>//</span><span>可访问

//$a->$name;//报错

//$a->stc_pd();//不可访问

//$a->stc_pe();//不可访问</span>





<span>/*</span><span>*******************
          | 可以 通过 className::functionNmae() 访问
public    | 
          | 可以 通过实例 $obj->functionName() 访问        

          | 不可 通过 className::functionNmae() 访问
protected | 
          | 不可 通过实例 $obj->functionName() 访问    

          | 不可 通过 className::functionNmae() 访问
private   | 
          | 不可 通过实例 $obj->functionName() 访问    
*******************</span><span>*/</span>
로그인 후 복사

二、直接访问(self::functionNmae())

结论:

self::functionNmae();需是静态方法(三种模式都可)

<?<span>php
</span><span>/*</span><span>*
 * 
 * @authors HG (hg0728@qq.com)
 * @date    2015-05-26 17:18:50
 * @version 1.0
 </span><span>*/</span>
<span>header</span>("Content-type:text/html;charset=utf-8"<span>);

</span><span>class</span><span> C {
    </span><span>static</span> <span>$name</span> = "静态变量"<span>;  
    </span><span>static</span> <span>public</span> <span>function</span><span> stc_pc(){        
        </span><span>echo</span> '静态公共方法<br>'<span>;
        self</span>::<span>stc_pd();
        self</span>::<span>stc_pe();
        self</span>::<span>stc();
        </span><span>//</span><span>self::func();//报错但有结果</span>
<span>        
    }

    </span><span>static</span> <span>protected</span> <span>function</span><span> stc_pd(){
        </span><span>echo</span> '静态受保护方法<br>'<span>;
    }
    </span><span>static</span> <span>private</span> <span>function</span><span> stc_pe(){        
        </span><span>echo</span> '静态受保护方法<br>'<span>;
    }

    </span><span>static</span> <span>function</span><span> stc(){        
        </span><span>echo</span> '静态普通方法<br>'<span>;
    }
    </span><span>function</span><span> func(){        
        </span><span>echo</span> '普通方法<br>'<span>;
    }
    
}


C</span>::<span>stc_pc();


</span><span>/*</span><span>*****

self::functionNmae();需是静态方法(三种模式都可)

*****</span><span>*/</span>
로그인 후 복사

三、通过子类访问父类(parent::functionNmae()和实例化)

结论:

parent::functionNmae(); private不可访问
$obj->functionNmae(); private不可访问

<?<span>php
</span><span>/*</span><span>*
 * 
 * @authors HG (hg0728@qq.com)
 * @date    2015-05-26 17:18:50
 * @version 1.0
 </span><span>*/</span>
<span>header</span>("Content-type:text/html;charset=utf-8"<span>);
</span><span>include</span> '/class_a.php'<span>;

</span><span>class</span> B <span>extends</span><span> A{
    
    </span><span>static</span> <span>public</span> <span>function</span><span> index(){        
        parent</span>::<span>stc_pc();
        parent</span>::<span>stc();
        parent</span>::<span>stc_pd();
        parent</span>::<span>$name</span>;<span>//</span><span>不报错
        //parent::stc_pe();//不可访问</span>

        <span>echo</span> '<br><br>通过实例化访问<br><br>'<span>;

        </span><span>$a</span> = <span>new</span><span> A();
        </span><span>$a</span>-><span>stc_pc();
        </span><span>$a</span>-><span>stc();
        </span><span>$a</span>-><span>stc_pd();
        </span><span>$a</span>-><span>$name</span>;<span>//</span><span>报错
        //$a->stc_pe();//不可</span>
<span>    }
    
}


B</span>::<span>index();

</span><span>/*</span><span>*****

在子类中通过:
parent::functionNmae(); private不可访问
$obj->functionNmae(); private不可访问

*****</span><span>*/</span>
로그인 후 복사

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1006439.htmlTechArticlepublic、protected、private的区别,protectedprivate 通过className::functionNmae()、实例化、parent、self得出结论 className::functionNmae() 需是静态方法(public模...
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿