汇才同飞教育汇才同飞学习平台

 找回密码
 立即注册
搜索
热搜: 活动 交友 javakc
 › for › 日志

设计一个员工类Employee   

2019-08-02 13:27:01 查看(96) 回复(0)

设计一个员工类Employee,具有员工编号、姓名、性别、住址、出生日期属性,覆盖Object的equals和toString方法。在测试类中,分别创建两个员工对象,比较是否相等,并打印员工信息。

将字符串“12-007,张三,男,上海浦东新区15号,1983-2-6”中的信息抽取出来,封装到创建的员工对象中。

package huicai4;

import java.util.Date;

public class Employee {

 //员工编号、姓名、性别、住址、出生日期属性
 private int no;
 private String name;
 private String sex;
 private String address;
 private Date birth;
 public int getNo() {
  return no;
 }
 public void setNo(int no) {
  this.no = no;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 public Date getBirth() {
  return birth;
 }
 public void setBirth(Date birth) {
  this.birth = birth;
 }
 @Override
 public String toString() {
  return "Employee [no=" + no + ", name=" + name + ", sex=" + sex + ", address=" + address + ", birth=" + birth
    + "]";
 }
 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + ((address == null) ? 0 : address.hashCode());
  result = prime * result + ((birth == null) ? 0 : birth.hashCode());
  result = prime * result + ((name == null) ? 0 : name.hashCode());
  result = prime * result + no;
  result = prime * result + ((sex == null) ? 0 : sex.hashCode());
  return result;
 }
 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  Employee other = (Employee) obj;
  if (address == null) {
   if (other.address != null)
    return false;
  } else if (!address.equals(other.address))
   return false;
  if (birth == null) {
   if (other.birth != null)
    return false;
  } else if (!birth.equals(other.birth))
   return false;
  if (name == null) {
   if (other.name != null)
    return false;
  } else if (!name.equals(other.name))
   return false;
  if (no != other.no)
   return false;
  if (sex == null) {
   if (other.sex != null)
    return false;
  } else if (!sex.equals(other.sex))
   return false;
  return true;
 }
}



package huicai4;

import java.util.Date;

public class EmployeeTest {

 public static void main(String[] args) {
  // TODO Auto-generated method stub


  Employee e=new Employee();
  e.setNo(12-007);
  e.setName("张三");
  e.setSex("男");
  e.setAddress("上海浦东新区15号");
  e.setBirth(new Date(1983-2-6));
  
  Employee e1=new Employee();
  e1.setNo(12-007);
  e1.setName("张三");
  e1.setSex("男");
  e1.setAddress("上海浦东新区15号");
  e1.setBirth(new Date(1983-2-6));
  
  System.out.println(e.toString());
  System.out.println(e1.toString());
  System.out.println(e.equals(e1));
 }

}

评论 (0 个评论)

facelist

全部作者的其他最新日志



站点统计|举报|Archiver|手机版|小黑屋|Comsenz Inc.

GMT+8, , Processed in 0.195171 second(s), 9 queries .

Powered by javakc! X1.0

© 2004-2014 javakc

备案号:京ICP备17025800号-2

返回顶部