class Student
{
private String name;
private int age;
public Student(String name,int age) {
this.name = name;
this.age = age;
}
public void show() {
System.out.println("name="+name+";age="+age);
}
}
class Undergraduce extends Student
{
private String degree;
public Undergraduce(String name,int age,String degree) {
super(name,age);
this.degree=degree;
}
public void show() {
System.out.println("name="+name+",age="+age+"degree="+degree);//这个地方报错: 错误: name可以在 Student中访问private
}
}
class Test
{
public static void main(String[] args) {
Undergraduce c=new Undergraduce("卓明钊",22,"硕士");
c.show();
Student b=new Student("卓明钊",22);
b.show();
Undergraduce c1=new Undergraduce("卓明钊",22,"硕士");
//c1.show();
}
}
=============================================================================
问题就是:子类不能继承父类构造方法这我们知道,当把父类的属性私有话以后,我在子类里面创建一个有参的构造方法,打印输出的时候难道不是输出我子类的属性?这里为什么会错?我要怎么样做才能在子类中获取父类的私有属性
请详尽的解答,谢谢! |
|