类继承问题。。。。
class person
{
private String name = "lucy";
private int age;
}
class student extends person
{
}
class superDemo1
{
public static void main(String[] args)
{
}
}
请问子类student有name字段吗,如果有,他的值是"lucy"吗
有的解释是父类的私有字段子类是可以继承的,只是不可以读取,我现在就想知道子类student有name字段吗,如果有,他的值是"lucy"吗?
[解决办法]
以代码为证:
public class Person {
private String name = "lucy";
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Student extends Person {
public static void main(String[] args)
{
Student s =new Student();
System.out.println(s.getName());
}
}
结果是: lucy