一道简单的JAVA编程题,各位大神些,帮帮忙。

定义一个Person类,含:姓名、性别、年龄等字段;继承Person类设计Teacher类,增加:职称、部门等字段;继承Person类设计Student类,增加:学号、入学时间、专业等字段。定义各类的构造方法和toString()方法,并分别创建对象进行测试。

public class main
{
public static void main(String[] args)
{
Person p = new Person("Parker","Male",17);
Teacher t = new Teacher("John","Male",46,"Professor","Maths");
Student s = new Student("Mary","Female",18,"12345","2010/9/1","Physics");
System.out.println(p);
System.out.println(t);
System.out.println(s);
}
}
class Person
{
public Person(String n, String s, int a)
{
name = n;
sex = s;
age = a;
}
public String toString()
{
String temp;
temp = "name: ".concat(name);
temp = temp.concat(", sex: ".concat(sex));
temp = temp.concat(", age: ".concat(Integer.toString(age)));
return temp;
}
public String name;
public String sex;
public int age;
}

class Teacher extends Person
{
public Teacher(String n, String s, int a, String p, String d)
{
super(n,s,a);
position = p;
department = d;
}
public String position;
public String department;
public String toString()
{
String temp = super.toString();
temp = temp.concat(", position: ".concat(position));
temp = temp.concat(", department: ".concat(department));
return temp;
}
}

class Student extends Person
{
public Student(String n, String s, int a, String i, String e, String m)
{
super(n,s,a);
id = i;
entranceDate = e;
major = m;
}
public String toString()
{
String temp = super.toString();
temp = temp.concat(", id: ".concat(id));
temp = temp.concat(", entranceDate: ".concat(entranceDate));
temp = temp.concat(", major: ".concat(major));
return temp;
}
public String id;
public String entranceDate;
public String major;
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答