import java.util.Scanner;
class Person //基类
{
private String name;
private int age;
String getName()
{
return name;
}
void setName(String name)
{
this.name=name;
}
int getAge()
{
return age;
}
void setAge(int age)
{
this.age=age;
}
public Person()
{
System.out.println("调用了个人类的构造方法。");
}
}
class Student1 extends Person //Student1是派生类
{
public Student1()
{
System.out.println("调用了学生类的构造方法。");
}
}
public class exa_02 {
public static void main(String []args)
{
Student1 stu=new Student1();
Scanner reader=new Scanner(System.in);
System.out.print("请输入学生的姓名和年龄:");
String name1=reader.next();
int age1=reader.nextInt();
stu.setName(name1);
stu.setAge(age1);
System.out.println(stu.getName()+" ");
System.out.println(stu.getAge()+" ");
reader.close();
}
}
运行结果: