Flask接口签名sign原理与实例代码浅析
266
2022-12-05
Java访问修饰符原理及代码解析
一、概述
访问等级比较:public > protected > default > private
无论是方法还是成员变量,这四种访问权限修饰符作用都一样
public:不管包外包内,所有类(子类+非子类)均可使用
protected
包内:所有类可使用
包外:子类可使用,非子类不可使用
default
包内:所有类可使用
包外:所有类不可使用
private:仅本类可使nQlESdK用
二、示例代码
Test包内的父类Permission
public class Permission {
private int privateValue = 1;
public int publicValue = 1;
protected int protectedValue = 1;
int defaultValue = 1;
void defaultFunc(){
System.out.println("This is a default function");
}
public void publicFunc(){
System.out.println("This is a public function");
}
protected void protectedFunc() {
System.out.printlnhttp://("This is a protected function");
}
private void privateFunc(){http://
System.out.println("This is a private function");
}
}
Test包内的子类SubPermission
public class SubPermission extends Permission{
public void permissionTest(){
http:// // public, protected, default function and variable can be used
System.out.println("this is a default value:" + this.defaultValue);
System.out.println("this is a public value:" + this.publicValue);
System.out.println("this is a protected value:" + this.protectedValue);
this.publicFunc();
this.protectedFunc();
this.defaultFunc();
}
}
Test包外的子类
import base.Test.Permission;
public class Demo extends Permission {
public static void main(String[] args) {
// if is not a subclass, only public function and variable can be used
Permission obj = new Permission();
obj.publicFunc();
System.out.println("this is a public value:" + obj.publicValue);
// if is a subclass, public and protected function and variable can ba used
Demo demo = new Demo();
demo.publicFunc();
demo.protectedFunc();
System.out.println("this is a public value:" + demo.publicValue);
System.out.println("this is a protected value:" + demo.protectedValue);
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~