【项目三】Student类和TestStudent类

题目描述

  • 编写一个完整的Application程序,包含类Student、TestStudent具体要求如下:
  1. Student类:
- 属性:	 
 		- number:String, 学号;	 
 		- name:String, 姓名; 
 		- sex:char,性别;	
 		- specialty:String,专业; 
 		- address:String,家庭地址; 
- 方法:	
	- 构造函数:
		- 无参格式(各数据设置为默认值);
		- 有参格式(利用参数对学号、姓名、专业进行设置); 
	- 访问器/修改器:对各属性数据进行读取和设置;
	- public String toString():返回学生的各项信息 public  boolean equals(Students):基于学号判断当前对象与参数对象是否为同一个学生;
  1. TestStudent作为主类完成测试功能。

代码实现

  1. 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;
		}
	}

}
  1. 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("学号相同的不是同一个对象");
		}
	}
}

运行结果展示

【项目三】Student类和TestStudent类_第1张图片


本节完!

更多信息交流请加QQ:1406073270

你可能感兴趣的:(#,java基础实操,java)