李四Employee
package diy.lisi;
import java.time.LocalDate;
public class Employee {
private final int age; // 年龄: 一般是个人隐私
public final String name; // 姓名: 姓名通常是公开且不变的
final LocalDate hireDay = LocalDate.now(); // 入职日期内部可以知道
public Employee(int age, String name) {
this.age = age;
this.name = name;
}
// ...
public static void main(String[] args) {
Employee e = new Employee(18, "张三");
System.out.println(e.age); // => 18
System.out.println(e.name); // => 张三
System.out.println(e.hireDay); // => 2024-10-20
}
}
李四MyTest
package diy.lisi;
public class MyTest {
public static void main(String[] args) {
Employee e = new Employee(18, "张三");
System.out.println(e.name); // => 张三
System.out.println(e.hireDay); // => 2024-10-20
// System.out.println(e.age);
}
}
diy.MyTest
package diy;
import diy.lisi.Employee;
public class MyTest {
public static void main(String[] args) {
Employee e = new Employee(18, "张三");
System.out.println(e.name); // => 张三
// System.out.println(e.age);
// System.out.println(e.hireDay);
}
}
要求
- 能解释两个MyTest中为什么访问不了相应的字段?并给出可能的解决方法
- 在Employee类中添加公开的访问器,使得在三个main方法中都能顺利读取所有字段值