public class educoder04{
public static void main(String[] args) {
// 实例化子类对象s,调用talk()方法打印信息
/********* begin *********/
Student s = new Student("张三",18,"哈佛大学");
s.talk();
/********* end *********/
}
}
class Person {
/********* begin *********/
String name;
int age;
Person(String name,int age){
this.name = name;
this.age = age;
}
void talk(){
System.out.print("我是:"+name+",今年:"+age+"岁");
}
/********* end *********/
}
class Student extends Person {
/********* begin *********/
String school;
Student(String name,int age,String school){
super(name,age);
this.school = school;
}
void talk(){
System.out.print("我是:"+name+",今年:"+age+"岁"+",我在"+school+"上学");
}
/********* end *********/
}
测试结果:
我是:张三,今年:18岁,我在哈佛大学上学