(1)编写一个Java程序,输出个人的基本信息,如:班级、学号、姓名、性别、籍贯、毕业中学、个人特长等信息。
时间: 2024-09-20 14:01:33 浏览: 34
在Java中,你可以创建一个名为`PersonalInfo`的类,它包含所有个人信息的属性,并提供一个构造函数和适当的getter和setter方法以便获取和设置这些信息。以下是一个简单的示例:
```java
public class PersonalInfo {
private String className; // 班级
private int studentID; // 学号
private String name; // 姓名
private char gender; // 性别 ('M'或 'F')
private String birthplace; // 籍贯
private String highSchool; // 毕业中学
private String specialSkill; // 个人特长
// 构造函数
public PersonalInfo(String className, int studentID, String name, char gender,
String birthplace, String highSchool, String specialSkill) {
this.className = className;
this.studentID = studentID;
this.name = name;
this.gender = gender;
this.birthplace = birthplace;
this.highSchool = highSchool;
this.specialSkill = specialSkill;
}
// Getter and Setter methods
public String getClassName() { return className; }
public void setClassName(String className) { this.className = className; }
public int getStudentID() { return studentID; }
public void setStudentID(int studentID) { this.studentID = studentID; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public char getGender() { return gender; }
public void setGender(char gender) { this.gender = gender; }
public String getBirthplace() { return birthplace; }
public void setBirthplace(String birthplace) { this.birthplace = birthplace; }
public String getHighSchool() { return highSchool; }
public void setHighSchool(String highSchool) { this.highSchool = highSchool; }
public String getSpecialSkill() { return specialSkill; }
public void setSpecialSkill(String specialSkill) { this.specialSkill = specialSkill; }
// 主要方法用于输出信息
@Override
public String toString() {
return "班级: " + className + "\n学号: " + studentID + "\n姓名: " + name +
"\n性别: " + gender + "\n籍贯: " + birthplace + "\n毕业中学: " + highSchool +
"\n个人特长: " + specialSkill;
}
}
// 在主程序中创建并打印实例
public static void main(String[] args) {
PersonalInfo person = new PersonalInfo("一班", 001, "张三", 'M', "北京", "实验中学", "编程");
System.out.println(person);
}
```
当你运行这个程序,它会输出一个包含个人基本信息的对象。
阅读全文