how to use public private protected in PHP

public 表示全局,类内部外部子类都可以访问;private表示私有的,只有本类内部可以使用;protected表示受保护的,只有本类或子类或父类中可以访问;

<?
//父类
class father{
public function a(){
  echo "function a";
}
private function b(){
  echo "function b";
}
protected function c(){
  echo "function c";
}
}
//子类
class child extends father{
  function d(){
    parent::a();//调用父类的a方法
  }
  function e(){
    parent::c(); //调用父类的c方法
  }
function f(){
    parent::b(); //调用父类的b方法
  }

}
$father=new father();
$father->a();
$father->b(); //显示错误 外部无法调用私有的方法 Call to protected method father::b()
$father->c(); //显示错误 外部无法调用受保护的方法Call to private method father::c()

$chlid=new child();
$chlid->d();
$chlid->e();
$chlid->f();//显示错误 无法调用父类private的方法 Call to private method father::b()
?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To make the variable value persistent after a screen reorientation, we need to save the state of the MainActivity instance. We can achieve this by using the onSaveInstanceState() method and the onRestoreInstanceState() method. In the onSaveInstanceState() method, we can save the value of the integer variable into the Bundle object. This Bundle object will be passed to the onRestoreInstanceState() method when the activity is recreated. In the onRestoreInstanceState() method, we can retrieve the saved value from the Bundle object and set it back to the integer variable. Here is an example code snippet: ``` public class MainActivity extends AppCompatActivity { private static final String VAR_KEY = "var_key"; private int myVar = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState != null) { myVar = savedInstanceState.getInt(VAR_KEY, 0); } Button myButton = findViewById(R.id.my_button); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myVar++; } }); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(VAR_KEY, myVar); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); myVar = savedInstanceState.getInt(VAR_KEY, 0); } } ``` In this example, we define a static final key for the variable in the Bundle object. In the onCreate() method, we check if there is a saved instance state and retrieve the saved value if there is one. We also set the onClickListener for the button to modify the value of the integer variable. In the onSaveInstanceState() method, we save the value of the integer variable into the Bundle object using the key we defined earlier. In the onRestoreInstanceState() method, we retrieve the saved value from the Bundle object and set it back to the integer variable.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值