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

2024-11-25 23:17:46
推荐回答(1个)
回答1:

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;
}