深度理解Java访问修饰符
作者:dz小伟 时间:2021-10-26 17:16:08
一、作用及种类
UML类图
建模语言或标准建模语言
类的属性、操作中的可见性使用+、#、-分别表示public、protected、private
作用:信息隐藏
防止用户意外修改数据,使模块易于维护和使用有哪些修饰符呢?
private:只有该类可以访问
protected:该类及其子类的成员可以访问,同一个包中的类也可以访问
public:该类或非该类可以访问
默认:同一个包中的类可以访问
可以作用于类,成员,方法
二、深度理解
同一个类
就是同一个类里面的,类相当于结构体
同一个包
圈中的就是包
三、代码理解
属性封装
class Student{//class 相当于c语言的struct
int age;
String name;
double score;
private int sex;//不同类,无法访问这个成员
public void cs(int a){
sex = a;
}
void introduce(){
System.out.println("sex="+sex+",age="+ age +",name=" + name + ",score=" + score);
}
void testFunc(){
System.out.println("testFunc");
}
}
public class Test {
public static void main(String[] args) {
Student stu1 = new Student();
//stu1.sex = 1; 因为类里面使用了private,不同类就无法访问
stu1.cs(0);//可以使用方法,间接修改成员
stu1.age = 22;
stu1.name = "xiaowei";
stu1.score = 100;
stu1.introduce();
stu1.testFunc();
}
}
/*
sex=0,age=22,name=xiaowei,score=100.0
testFunc
*/
方法封装
class Student{//class 相当于c语言的struct
private int age;
String name;
double score;
private int sex;
private void myage(int myage){//不可见的方法,可以在别的方法中使用达到更改属性的目的
age = myage;
}
public void setage(int realage){//更改age
age = realage;
}
public int getage(){//输出age,方法里偷偷使用myage,输出永远是18
myage(18);
return age;
}
void cs(int a){
sex = a;
}
void introduce(){
System.out.println("sex="+sex+",age="+ age +",name=" + name + ",score=" + score);
}
void testFunc(){
System.out.println("testFunc");
}
}
public class Test {
public static void main(String[] args) {
Student stu1 = new Student();
//stu1.sex = 1;
stu1.cs(0);
//stu1.age = 22;
stu1.setage(36);//更改年龄36
stu1.name = "xiaowei";
stu1.score = 100;
stu1.introduce();
stu1.testFunc();
System.out.println("age="+stu1.getage());//输出年龄是18,方法中偷偷调用了不可见的方法myage
}
}
/*
sex=0,age=36,name=xiaowei,score=100.0
testFunc
age=18
*/
来源:https://blog.csdn.net/m0_51247005/article/details/117674783
标签:Java,访问修饰符
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
理解java设计模式之建造者模式
2023-03-13 14:48:55
java自定义注解验证手机格式的实现示例
2023-06-24 10:42:44
教你使用java实现去除各种空格
2022-09-21 21:27:07
基于java实现租车管理系统
2022-02-08 12:48:49
![](https://img.aspxhome.com/file/2023/8/73038_0s.jpg)
Java基础将Bean属性值放入Map中的实例
2023-10-11 13:57:40
C#获取网页源代码的方法
2023-06-19 05:43:44
EasyExcel实现导入+各种数据校验功能
2022-05-12 13:05:26
![](https://img.aspxhome.com/file/2023/1/71921_0s.png)
IntelliJ IDEA2019实现Web项目创建示例
2023-06-05 00:29:33
![](https://img.aspxhome.com/file/2023/3/62333_0s.jpg)
Java特性队列和栈的堵塞原理解析
2023-10-13 14:15:55
elasticsearch索引index之engine读写控制结构实现
2021-12-22 00:08:07
![](https://img.aspxhome.com/file/2023/9/61299_0s.jpg)
SpringMVC接收复杂集合对象(参数)代码示例
2023-01-29 18:33:51
一文搞懂Spring中的注解与反射
2023-11-11 11:38:18
![](https://img.aspxhome.com/file/2023/8/58758_0s.jpg)
浅谈java 增强型的for循环 for each
2023-03-30 18:51:05
MyBatis-Plus找不到Mapper.xml文件的几种解决方法
2023-11-24 03:37:52
ActiveMQ安装及部署教程图解
2023-11-17 18:01:46
![](https://img.aspxhome.com/file/2023/3/59483_0s.png)
浅析Java中的内存泄漏
2023-12-19 02:29:02
![](https://img.aspxhome.com/file/2023/3/70993_0s.jpg)
如何利用Spring把元素解析成BeanDefinition对象
2023-11-23 05:17:16
![](https://img.aspxhome.com/file/2023/9/59939_0s.png)
SpringBoot+Jpa项目配置双数据源的实现
2022-11-01 14:05:04
![](https://img.aspxhome.com/file/2023/0/66540_0s.png)
JDBC核心技术详解
2023-12-09 12:22:28
![](https://img.aspxhome.com/file/2023/9/63169_0s.png)
OpenCV实现直线检测并消除
2023-07-12 20:44:36
![](https://img.aspxhome.com/file/2023/3/92493_0s.jpg)