学习笔记;Java四种不同访问权限 Public、Private、protected以及友好变量

在这里插入图片描述

私有变量和私有方法;用private修饰;其他类中不能创建该类的对象也不能使用该类的私有对象和方法
假若父类定义了私有变量,该变量不能 被子类所继承,当如果父类中有可继承的方法调用了该私有变量,我们在子类中也可以通过继承该方法而调用这个私有变量。
Class Tom{
Private float age;
Private float People(float a){
}
}
Class P{
Tom tom=new Tom();//非法;
tom.age;非法
}

共有变量和共有方法。用public修饰,其他类创建了该对象后能使用该对象的方法和变量
Class Tom{
Prublic float age;
Public float People(float a){
}
}
Class P{
Tom tom=new Tom();//合法;
tom.age;合法
}

友好变量 ,没有修饰词,同一个的包中其他类创建了该对象后能使用该对象的方法和变量
而是用import导入的则不能使用。
Class Tom{
Prublic float age;
Public float People(float a){
}
}
Class P{
Tom tom=new Tom();//合法;
tom.age;合法
}

保留 保护变量和方法。用protected修饰,大致与友好变量相同、
区别在于 友好变量和私有变量不能被继承而公共变量和保护变量能被继承

猜你喜欢

转载自blog.csdn.net/Eysunvoes/article/details/88090371
今日推荐