java定义一所高校包含的老师类学生类职员类工人类网站类举例代码打印
时间: 2023-05-30 13:03:25 浏览: 109
以下是Java定义一所高校包含的老师类、学生类、职员类、工人类和网站类的举例代码,并打印输出它们的信息:
```
// 定义人类
class Person {
String name; // 姓名
int age; // 年龄
String gender; // 性别
public Person(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public void printInfo() {
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
System.out.println("性别:" + gender);
}
}
// 定义老师类
class Teacher extends Person {
String subject; // 教授学科
public Teacher(String name, int age, String gender, String subject) {
super(name, age, gender);
this.subject = subject;
}
public void printInfo() {
super.printInfo();
System.out.println("教授学科:" + subject);
}
}
// 定义学生类
class Student extends Person {
String major; // 专业
public Student(String name, int age, String gender, String major) {
super(name, age, gender);
this.major = major;
}
public void printInfo() {
super.printInfo();
System.out.println("专业:" + major);
}
}
// 定义职员类
class Staff extends Person {
String department; // 部门
public Staff(String name, int age, String gender, String department) {
super(name, age, gender);
this.department = department;
}
public void printInfo() {
super.printInfo();
System.out.println("部门:" + department);
}
}
// 定义工人类
class Worker extends Person {
String job; // 工种
public Worker(String name, int age, String gender, String job) {
super(name, age, gender);
this.job = job;
}
public void printInfo() {
super.printInfo();
System.out.println("工种:" + job);
}
}
// 定义网站类
class Website {
String name; // 网站名称
String url; // 网站地址
public Website(String name, String url) {
this.name = name;
this.url = url;
}
public void printInfo() {
System.out.println("网站名称:" + name);
System.out.println("网站地址:" + url);
}
}
// 测试
public class Test {
public static void main(String[] args) {
Teacher teacher = new Teacher("张三", 35, "男", "数学");
Student student = new Student("李四", 20, "女", "计算机科学与技术");
Staff staff = new Staff("王五", 30, "男", "行政部门");
Worker worker = new Worker("赵六", 40, "女", "清洁工");
Website website = new Website("百度", "https://www.baidu.com/");
teacher.printInfo();
System.out.println();
student.printInfo();
System.out.println();
staff.printInfo();
System.out.println();
worker.printInfo();
System.out.println();
website.printInfo();
}
}
```
输出结果为:
```
姓名:张三
年龄:35
性别:男
教授学科:数学
姓名:李四
年龄:20
性别:女
专业:计算机科学与技术
姓名:王五
年龄:30
性别:男
部门:行政部门
姓名:赵六
年龄:40
性别:女
工种:清洁工
网站名称:百度
网站地址:https://www.baidu.com/
```
阅读全文