forked from CPU-Code/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcall_private.java
More file actions
66 lines (52 loc) · 1.8 KB
/
Copy pathcall_private.java
File metadata and controls
66 lines (52 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* @由于个人水平有限, 难免有些错误, 还请指点:
* @Author: cpu_code
* @Date: 2020-09-11 14:39:44
* @LastEditTime: 2020-09-11 14:41:28
* @FilePath: \java\object\call_private.java
* @Gitee: [https://gitee.com/cpu_code](https://gitee.com/cpu_code)
* @Github: [https://github.com/CPU-Code](https://github.com/CPU-Code)
* @CSDN: [https://blog.csdn.net/qq_44226094](https://blog.csdn.net/qq_44226094)
* @Gitbook: [https://923992029.gitbook.io/cpucode/](https://923992029.gitbook.io/cpucode/)
*/
public class call_private {
public static void main(String[] args) {
Person ming = new Person();
ming.setName("cpucode"); // 设置name
ming.setBirth(2008);
System.out.println(ming.getName());
System.out.println(ming.getAge());
}
static class Person {
private String name;
private int birth;
// 方法间接修改private字段
public String getName() {
// 隐含的变量 this,它始终指向当前实例
return this.name;
}
public void setName(String name) {
// 不允许传入null和空字符串
if (name == null || name.isBlank()) {
throw new IllegalArgumentException("invalid name");
}
// 前面的this不可少,少了就变成局部变量name了
this.name = name;
}
public int getAge() {
// 方法可以封装一个类的对外接口
return calcAge(2020); // 调用private方法
}
public void setBirth(int birth) {
this.birth = birth;
}
// private方法: 内部方法是, 不允许外部调用
private int calcAge(int currentYear) {
return currentYear - this.birth;
}
}
}
/*
cpucode
12
*/