1.学生和老师都会有work方法,学生的工作是学习,老师的工作是教书,我利用了一个接口来实现;
2.同时,老师和学生都是人,并且都有姓名,姓名,年龄和身高等特征,我用了一个继承来实现,子类对于父类有扩展,如老师有学工号,职称,专业和工资的特征以及批改作业的行为,学生有学号,年级,专业和成绩以及写作业的行为
package com.java.student;
public interface Work {
public abstract void work();
}
package com.java.student;
public class Person {
private String name;
private String gender;
private int age;
private double height;
public Person() {
}
public Person(String name, String gender, int age, double height) {
this.name = name;
this.gender = gender;
this.age = age;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
package com.java.student;
public class Student extends Person implements Work{
private String ID;
private String grade;
private String major;
private double score;
public Student() {
}
public Student(String name, String gender, int age, double height) {
super(name, gender, age, height);
}
public Student(String ID, String grade, String major, double score) {
this.ID = ID;
this.grade = grade;
this.major = major;
this.score = score;
}
public Student(String name, String gender, int age, double height, String ID, String grade, String major, double score) {
super(name, gender, age, height);
this.ID = ID;
this.grade = grade;
this.major = major;
this.score = score;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public void work() {
System.out.println("学生在认真学习!!!");
}
public void write(){
System.out.println("学生在认真写作业!");
}
}
package com.java.student;
public class Teacher extends Person implements Work{
private String ID;
private String title;
private String major;
private double wage;
public Teacher() {
}
public Teacher(String name, String gender, int age, double height) {
super(name, gender, age, height);
}
public Teacher(String ID, String title, String major, double wage) {
this.ID = ID;
this.title = title;
this.major = major;
this.wage = wage;
}
public Teacher(String name, String gender, int age, double height, String ID, String title, String major, double wage) {
super(name, gender, age, height);
this.ID = ID;
this.title = title;
this.major = major;
this.wage = wage;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
@Override
public void work() {
System.out.println("老师正在认真教学生!!!");
}
public void correct(){
System.out.println("老师正在认真批改学生的作业!");
}
}
package com.java;
import com.java.student.Person;
import com.java.student.Student;
import com.java.student.Teacher;
public class Main {
public static void main(String[] args) {
Person stu = new Student("张三","男",18,188);
System.out.println("姓名:" + stu.getName() +" 性别:" + stu.getGender() + " 年龄:" + stu.getAge() + " 身高:" + stu.getHeight());
((Student) stu).work();
Student stu1 = new Student("1001","大一","食品专业",88);
stu1.setName("舒望");
System.out.print("姓名:" + stu1.getName());
System.out.println(" 学号:" + stu1.getID() + " 年级:" + stu1.getGrade() + " 专业:" + stu1.getMajor() +" 分数:" + stu1.getScore());
stu1.write();
Person tec = new Teacher("莉莉","女",58,163);
System.out.println("姓名:" + stu.getName() +" 性别:" + stu.getGender() + " 年龄:" + stu.getAge() + " 身高:" + stu.getHeight());
Teacher tec1 = new Teacher("2014","副教授","马克思学院",8888);
tec1.setName("王五");
System.out.print("姓名:" + tec1.getName());
System.out.println(" 职工号:" + tec1.getID() + " 职称:" + tec1.getTitle() + " 专业:" + tec1.getMajor() +" 薪水:" + tec1.getWage());
tec1.work();
tec1.correct();
}
}
结果展示: