【项目三】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("学号相同的不是同一个对象");
		}
	}
}

运行结果展示

在这里插入图片描述


本节完!

更多信息交流请加QQ:1406073270
  • 8
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值