定义一个PersonA类,定义一个PersionA类的子类StudentA类,
再定义一个C1类,在main()方法中,生成StudentA的对象,使用程序运行结果显示:
你好,我是李四
我是计算机学院的学生
class PersonA
{
private String name;
public void setName(String newName)
{
name=newName;
}
public String getName()
{
return name;
}
}
class StudentA extends PersonA //继承PersonA类
{
private String department;
public void setDepartment(String newDepartment)
{
department=newDepartment;
}
public String getDepartment()
{
return department;
}
}class C1 //主类
{
public static void main(String[] args)
{
StudentA S1=new StudentA();
S1.setName("李四");//调用 setName()方法,传入参数"李四"
S1.setDepartment("计算机学院");//调用 setDepartment()方法,传入参数"计算机学院"
System.out.println("你好,我是" + S1.getName()); //显示s1姓名
System.out.println("我是" + S1.getDepartment() + "的学生"); //显示s1的系别
}
}
结果显示