package com.Object;
/*5.定义一个人类,包括属性:姓名、性别、年龄、国籍;包括方法:吃饭、睡觉,工作。
(1)根据人类,派生一个学生类,增加属性:学校、学号;重写工作方法(学生的工作是学习)。
(2)根据人类,派生一个工人类,增加属性:单位、工龄;重写工作方法(工人的工作是……自己想吧)。
(3)根据学生类,派生一个学生干部类,增加属性:职务;增加方法:开会。
(4)编写主函数分别对上述3类具体人物进行测试。*/
public class Test5 {
public static void main(String[] args) {
Student1 s = new Student1("张三",23,"中国","南大","007");
s.eat();
s.sleep();
s.work();
System.out.println("姓名:"+s.getName()+"年龄:"+s.getAge()+"国籍:"+s.getCon()+"学校:"+s.getSchool()+"学号:"+s.getID());
Worker w = new Worker("李四",25,"美国","黑马",3);
w.eat();
w.sleep();
w.work();
System.out.println("姓名:"+w.getName()+"年龄:"+w.getAge()+"国籍:"+w.getCon()+"单位:"+w.getUnits()+"工龄:"+w.getWage());
GoodStudent gs = new GoodStudent("王五",26,"英国","哈佛","033","班长");
gs.eat();
gs.sleep();
gs.work();
gs.conference();
System.out.println("姓名:"+gs.getName()+"年龄:"+gs.getAge()+"国籍:"+gs.getCon()+"学校:"+gs.getSchool()+"学号:"+gs.getID()+"职务:"+gs.getDuty());
}
}
class Person{
private String name;
private int age;
private String con;
public Person() {
super();
}
public Person(String name, int age, String con) {
super();
this.name = name;
this.age = age;
this.con = con;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the con
*/
public String getCon() {
return con;
}
/**
* @param con the con to set
*/
public void setCon(String con) {
this.con = con;
}
public void eat(){
System.out.println("吃饭");
}
public void sleep(){
System.out.println("睡觉");
}
public void work(){
System.out.println("工作");
}
}
class Student1 extends Person{
private String school;
private String ID;
public Student1() {
super();
}
public Student1(String name, int age, String con,String school,String ID) {
super(name, age, con);
this.school = school;
this.ID = ID;
}
/**
* @return the school
*/
public String getSchool() {
return school;
}
/**
* @param school the school to set
*/
public void setSchool(String school) {
this.school = school;
}
/**
* @return the iD
*/
public String getID() {
return ID;
}
/**
* @param iD the iD to set
*/
public void setID(String iD) {
ID = iD;
}
public void work(){
System.out.println("学习");
}
}
class Worker extends Person{
private String units;
private int wage;
public Worker() {
super();
}
public Worker(String name, int age, String con,String units,int wage) {
super(name, age, con);
this.units = units;
this.wage = wage;
}
/**
* @return the units
*/
public String getUnits() {
return units;
}
/**
* @param units the units to set
*/
public void setUnits(String units) {
this.units = units;
}
/**
* @return the wage
*/
public int getWage() {
return wage;
}
/**
* @param wage the wage to set
*/
public void setWage(int wage) {
this.wage = wage;
}
public void work(){
System.out.println("搬砖");
}
}
class GoodStudent extends Student1 {
private String duty;
public GoodStudent() {
super();
}
public GoodStudent(String name, int age, String con, String school,
String ID,String duty) {
super(name, age, con, school, ID);
this.duty = duty;
}
/**
* @return the duty
*/
public String getDuty() {
return duty;
}
/**
* @param duty the duty to set
*/
public void setDuty(String duty) {
this.duty = duty;
}
public void conference(){
System.out.println("开会");
}
}
|
|