定义一个人类,包括属性:姓名、性别、年龄、国籍;包括方法:吃饭、睡觉,工作。(1)根据人类,派生一个学生类,增加属性:学校、学号;重写工作方法(学生的工作是学习)。(2)根据人类,派生一个工人类,增加属性:单位、工龄;重写工作方法(工人的工作是……自己想吧)。(3)根据学生类,派生一个学生干部类,增加属性:职务;增加方法:开会。(4)编写主函数分别对上述3类具体人物进行测试。
package test;
class Person
{
String name;
char sex;
int age;
String nationality;
public Person(){}
public Person(String name,char sex,int age,String nationality)
{
this.name=name;
this.sex=sex;
this.age=age;
this.nationality=nationality;
}
public void behavio()
{
System.out.print("吃饭,睡觉,工作");
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", sex=" + sex +
", age=" + age +
", nationality='" + nationality + '\'' +
'}';
}
}
class Student extends Person
{
String school;
int stnumber;
public Student(String name,char sex,int age,String nationality,String school,int stnumber)
{
super(name,sex,age,nationality);
this.school=school;
this.stnumber=stnumber;
}
public void behavio()
{
super.behavio();
System.out.print(",学习");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", sex=" + sex +
", age=" + age +
", nationality='" + nationality + '\'' +
", school='" + school + '\'' +
", stnumber=" + stnumber +
'}';
}
}
class StudentCadrs extends Student
{
String position;
public StudentCadrs(String name,char sex,int age,String nationality,String school,int stnumber,String position)
{
super(name,sex,age,nationality,school,stnumber);
this.position=position;
}
public void behavio()
{
super.behavio();
System.out.println(",开会");
}
@Override
public String toString() {
return "StudentCadrs{" +
"name='" + name + '\'' +
", sex=" + sex +
", age=" + age +
", nationality='" + nationality + '\'' +
", school='" + school + '\'' +
", stnumber=" + stnumber +
", position='" + position + '\'' +
'}';
}
}
class Work extends Person
{
String department;
int workage;
public Work(String name,char sex,int age,String nationality,int workage)
{
super(name,sex,age,nationality);
this.workage=workage;
}
@Override
public void behavio()
{
super.behavio();
System.out.println(",喝酒");
}
@Override
public String toString() {
return "Work{" +
"name='" + name + '\'' +
", sex=" + sex +
", age=" + age +
", nationality='" + nationality + '\'' +
", department='" + department + '\'' +
", workage=" + workage +
'}';
}
}
public class work3 {
public static void main(String[] args) {
Student student=new Student("张三",'男',18,"中国","清华大学",202100857);
StudentCadrs studentCadrs=new StudentCadrs("张三",'男',18,"中国","清华大学",202100857,"学生会会长");
Work work=new Work("李四",'男',35,"中国",10);
System.out.println("个人信息:");
System.out.println("Student:"+student.toString());
System.out.print("Student个人生活:");
student.behavio();
System.out.println();
System.out.println("StudentCadrs:"+studentCadrs.toString());
System.out.print("StudentCadrs个人生活:");
studentCadrs.behavio();
System.out.println("Work:"+work.toString());
System.out.print("Work个人生活:");
work.behavio();
}
}