- public class Question2 {
- public static void main(String[] args) {
- Student s1=new Student("xiaoming","public XXX school");
- }
- }
- class Student extends Person{
- String school;
- Student(String name,String school){
- super.setName(name); //访问父类私有成员需要使用父类提供访部访问的方法
- //因为继承之后就相当于子类也拥有这个私有成员
- this.school=school;
- }
- Student(){
-
- }
- }
- class Person{
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
-
- }
复制代码 |