1. 创建学生类,属性包括:姓名(name)、年龄(age)、手机号(phoneNo)、身份证号(idCare)添加有参和无参构造方法、get和set方法,通过toString方法输出属性信息
时间: 2024-12-25 07:34:10 浏览: 3
创建一个名为`Student`的简单Java类,包含姓名(name)、年龄(age)、手机号(phoneNo)和身份证号(idCard)作为属性。该类将提供有参和无参的构造函数来初始化这些属性,以及对应的getter和setter方法用于获取和设置值。最后,我们将覆盖`toString()`方法以便于输出学生的详细信息。
```java
public class Student {
private String name;
private int age;
private String phoneNo;
private String idCard;
// 无参构造方法
public Student() {
this.name = "";
this.age = 0;
this.phoneNo = "";
this.idCard = "";
}
// 有参构造方法
public Student(String name, int age, String phoneNo, String idCard) {
this.name = name;
this.age = age;
this.phoneNo = phoneNo;
this.idCard = idCard;
}
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
// Overriding toString() method
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", phoneNo='" + phoneNo + '\'' +
", idCard='" + idCard + '\'' +
'}';
}
}
```
阅读全文