编写一个JAVA程序,一个Student类,能够显示学生的姓名,性别,年龄,学号,系部,专业,使用构造方法
时间: 2024-05-14 10:17:59 浏览: 117
下面是一个基本的JAVA程序,实现了一个Student类,包含了姓名、性别、年龄、学号、系部和专业等属性,并使用构造方法初始化这些属性。
```
public class Student {
private String name;
private String gender;
private int age;
private String id;
private String department;
private String major;
public Student(String name, String gender, int age, String id, String department, String major) {
this.name = name;
this.gender = gender;
this.age = age;
this.id = id;
this.department = department;
this.major = major;
}
public void display() {
System.out.println("姓名:" + this.name);
System.out.println("性别:" + this.gender);
System.out.println("年龄:" + this.age);
System.out.println("学号:" + this.id);
System.out.println("系部:" + this.department);
System.out.println("专业:" + this.major);
}
public static void main(String[] args) {
Student student = new Student("张三", "男", 20, "1001", "计算机系", "软件工程");
student.display();
}
}
```
运行结果:
```
姓名:张三
性别:男
年龄:20
学号:1001
系部:计算机系
专业:软件工程
```
阅读全文