为什么只输出了一个员工, 张三和21怎么没输出?
class Person
{
private String name;
private int age;
public Person()
{
}
public Person(String name, int age)
{
this.age = age;
this.name = name;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return age;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
class Employee extends Person
{
private String employee_no;
public Employee(String name, int age, String employee_no)
{
super(name,age);
this.employee_no = employee_no;
}
public void shuchu()
{
System.out.println(employee_no);
}
}
public class Test {
public static void main(String[] args) {
Employee E = new Employee("张三", 21, "员工");
Person P = new Person("张三", 21);
P.getAge();
P.getName();
E.shuchu();
}
}
------解决思路----------------------
同为初学者,抛砖引玉而已,大神出来给更详尽解答之前,先说点自己的拙见。
getName和getAge是获取了P的对象属性,但是并没有输出语句输出属于该对象的值,所以控制台没显示出来,主函数里加两句话就好了
int age=P.getAge();
String name=P.getName();
System.out.println(age+"\t"+name);
------解决思路----------------------
是这样的!