嗯,可以在父类中通过setter和getter方法来获取父类中被封装的name属性的值。- class Person
- {
- private String name;
- Person(String name)
- {
- this.name = name;
- }
- public void print()
- {
- System.out.println("姓名:"+name);
- }
- public String getName()
- {
- return name;
- }
- }
- class Student extends Person
- {
- Student(String name)
- {
- super(name);
- }
- }
- public class Demo
- {
- public static void main(String[] args)
- {
- Student s = new Student("张三");
- s.print();
- }
- }
复制代码 |