定义一个教师类及编写测试类使用教师类(先分析,再代码)
时间: 2024-01-15 08:02:51 浏览: 67
分析:
教师类应该包含以下属性:
- 姓名
- 年龄
- 性别
- 教龄
- 学科
教师类应该包含以下方法:
- 构造方法:用于对教师属性进行初始化
- 获取姓名方法:用于获取教师的姓名
- 获取年龄方法:用于获取教师的年龄
- 获取性别方法:用于获取教师的性别
- 获取教龄方法:用于获取教师的教龄
- 获取学科方法:用于获取教师所教授的学科
测试类应该包含以下方法:
- main 方法:用于创建教师对象并输出教师信息
代码:
教师类:
```java
public class Teacher {
private String name;
private int age;
private String gender;
private int teachingYears;
private String subject;
public Teacher(String name, int age, String gender, int teachingYears, String subject) {
this.name = name;
this.age = age;
this.gender = gender;
this.teachingYears = teachingYears;
this.subject = subject;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public int getTeachingYears() {
return teachingYears;
}
public String getSubject() {
return subject;
}
}
```
测试类:
```java
public class TeacherTest {
public static void main(String[] args) {
Teacher teacher = new Teacher("张三", 35, "男", 10, "数学");
System.out.println("姓名:" + teacher.getName());
System.out.println("年龄:" + teacher.getAge());
System.out.println("性别:" + teacher.getGender());
System.out.println("教龄:" + teacher.getTeachingYears());
System.out.println("学科:" + teacher.getSubject());
}
}
```
阅读全文