该Date构造方法已过时,所以改写了一下
import java.util.Date;
import java.util.GregorianCalendar;
public class School{
public static void main(String[] args){
Person person = new Person("张三", "男", 25);
Teacher teacher = new Teacher("张兰", "女", 24, "讲师", "英语");
Student student = new Student("李四", "男", 23, "1001", 2011, 11, 29, "计算机");
System.out.println(person.toString());
System.out.println(teacher.toString());
System.out.println(student.toString());
}
}
class Person{
public Person(String na, String s, int a){
name = na;
sex = s;
age = a;
}
public String toString(){
return "name:"+name+"
"+"sex:"+sex+"
"+"age:"+age+"";
}
private String name;
private String sex;
private int age;
}
class Teacher extends Person{
public Teacher(String na, String s, int a, String zc, String de){
super(na, s, a);
zhicheng = zc;
department = de;
}
public String toString(){
return ""+super.toString()+"
"+"zhicheng:"+zhicheng+"
"+"department:"+department+"";
}
private String zhicheng;
private String department;
}
class Student extends Person{
public Student(String na, String s, int a, String n, int year, int month, int day,
String ma){
super(na, s, a);
no = n;
GregorianCalendar calendar = new GregorianCalendar(year, month, day);
time = calendar.getTime();
major = ma;
}
public String toString(){
return ""+super.toString()+"
"+"no:"+no+"
"+"time:"+time+"
"+"major:"+major+"";
}
private String no;
private Date time;
private String major;
}