类名:Student 属性:姓名,年龄,班级,爱好,方法:输出学生相关信息; 类名:Teacher 属性:姓名,年龄,专业,方法:输出教员相关信息。 测试类test1:创建一个学生对象和一个教师对象,对各个属性赋值,输出相关信息
时间: 2024-03-03 17:47:16 浏览: 110
好的,这是一个简单的面向对象编程问题,我可以帮你解答。
首先,我们需要定义两个类:Student和Teacher。代码如下:
```
public class Student {
private String name;
private int age;
private String classroom;
private String hobby;
public Student(String name, int age, String classroom, String hobby) {
this.name = name;
this.age = age;
this.classroom = classroom;
this.hobby = hobby;
}
public void showInfo() {
System.out.println("姓名:" + name + ",年龄:" + age + ",班级:" + classroom + ",爱好:" + hobby);
}
}
public class Teacher {
private String name;
private int age;
private String major;
public Teacher(String name, int age, String major) {
this.name = name;
this.age = age;
this.major = major;
}
public void showInfo() {
System.out.println("姓名:" + name + ",年龄:" + age + ",专业:" + major);
}
}
```
然后,我们可以创建一个测试类Test1,来测试Student和Teacher的功能。代码如下:
```
public class Test1 {
public static void main(String[] args) {
Student student = new Student("小明", 18, "一班", "打篮球");
Teacher teacher = new Teacher("张老师", 35, "计算机科学");
student.showInfo();
teacher.showInfo();
}
}
```
运行Test1类的main方法,即可输出学生和教师的相关信息。输出结果如下:
```
姓名:小明,年龄:18,班级:一班,爱好:打篮球
姓名:张老师,年龄:35,专业:计算机科学
```
希望这个解答可以帮到你。
阅读全文