php class drip --- access modifiers public protected private

  • public can be inherited, can external access (that is, instantiate the object can be accessed directly)
  • protected protected, it can be inherited by subclasses and can not access external
  • private inheritance, not even think about external access
? <PHP 
class Coach
{
public Master $ = "Zhang";
protected Lover $ = "D cauldron";
Private LoveGame $ = "SM";
public Slogan function ()
{
. echo "~ ha ~ Houhou" "\ n-";
}
}

class the extends Coach CPC
{
public function the __construct ()
{
// $ this-> = $ Master Master;
echo" is my master __ I love "$ this" $ this-> master .. ". . -> lover "__ like playing" "\ n-";. # Description public protected attributes can be inherited from the parent class come
}
public function self_introduce (name $, $ Age)
{

echo "My name IS Hello ~" $. ... name "this year Christina" $ age "\ n";
}
}
$cpc = new cpc();
$cpc->slogan();
$ cpc-> self_introduce ( 'Chen Peichang', 21);
echo $ CPC-> Master; # Description public property can be accessed by an external (ie, instantiate the object can directly access)
>?

 Output:

My master is Zhang Ding cauldron __ __ I love love to play
Houhou ha ha ~ ~       # inherited slogan from the parent class () method
hello ~ my name is Christina Chen Peichang year 21  # subclasses self_introduce method

Zhang    # object directly access the parent class public property

Guess you like

Origin www.cnblogs.com/saintdingspage/p/10958274.html
Recommended