【项目三】Student类和TestStudent类
程序员文章站
2022-07-15 16:56:43
...
题目描述
- 编写一个完整的Application程序,包含类Student、TestStudent具体要求如下:
- Student类:
- 属性:
- number:String, 学号;
- name:String, 姓名;
- sex:char,性别;
- specialty:String,专业;
- address:String,家庭地址;
- 方法:
- 构造函数:
- 无参格式(各数据设置为默认值);
- 有参格式(利用参数对学号、姓名、专业进行设置);
- 访问器/修改器:对各属性数据进行读取和设置;
- public String toString():返回学生的各项信息 public boolean equals(Students):基于学号判断当前对象与参数对象是否为同一个学生;
- TestStudent作为主类完成测试功能。
代码实现
- Student.java
public class Student {
private String number;
private String name;
private char sex;
private String specialty;
private String address;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String number, String name, char sex, String specialty, String address) {
super();
this.number = number;
this.name = name;
this.sex = sex;
this.specialty = specialty;
this.address = address;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getSpecialty() {
return specialty;
}
public void setSpecialty(String specialty) {
this.specialty = specialty;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String toString() {
return "number=" + number + ", name=" + name + ", sex=" + sex + ", specialty=" + specialty
+ ", address=" + address;
}
public boolean equals(Student s) {
if (this.number.equals(s.number)) {
return true;
}else {
return false;
}
}
}
- StudentTest.java
public class StudentTest {
public static void main(String[] args) {
Student s1 = new Student("201820221","xiaoqi",'女',"物联网","河北");
System.out.println(s1.toString());
Student s2 = new Student("201820222","xiaowen",'女',"物联网","河北");
System.out.println(s2.toString());
Student s3 = new Student("201820221","xiaowen",'女',"物联网","河北");
System.out.println(s3.toString());
if (s1.equals(s3)) {
System.out.println("学号相同的是同一个对象");
}else {
System.out.println("学号相同的不是同一个对象");
}
}
}
运行结果展示
本节完!
更多信息交流请加QQ:1958917311
推荐阅读
-
ASP.NET MVC 主要的四种过滤器和三种具体实现类
-
mouseleave和mouseout,mouseenter和mouseover,mouseover和mousemove三类比较分析
-
PHP通过反射动态加载第三方类和获得类源码的实例
-
C++实验三——类和对象
-
Java基础知识(三)面向对象、类和对象、封装继承多态、构造方法、内部类、包装类
-
创建类Student和对象
-
c++基础-继承与派生,定义基类person和公有派生类student
-
(c++)类多重继承和派生 - person、student、teacher和graduate类
-
从Student类和Teacher类多重派生Graduate类
-
从Student类和Teacher类多重派生Graduate类