- 需求
- 定义一个(Person)类,包含属性:姓名,年龄,性别;写好相关的方法,能够构造的时候初始化或者后续修改属性值。
- 定义一个Employee类继承于Person类,包含新的属性:职位;写好相关的方法,能够构造的时候初始化或者后续修改属性值,重载equals方法,规则:当两个对象的姓名,年龄,性别,职位都相等时返回true,否则返回false。
- 写一个测试类进行相关测试(要求可以打印对象的信息,能够比较对象是否"相等")。
效果:
package zuoyejicheng;
public class Person {
String name;//姓名
int age;//年龄
String sex;//性别
public Person(String name, int age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
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 getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public boolean equals(String name1,String name2) {
return true;
}
}
package zuoyejicheng;
public class Employee extends Person {
String job;//职位
public Employee(String name, int age, String sex,String job) {
super(name, age, sex);
this.job=job;
// TODO Auto-generated constructor stub
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public boolean equals (Employee e) {
if (this.getName().equals(e.getName())&&this.getAge()==e.getAge()&&this.getSex().equals(e.getSex())&& this.getJob().equals(e.getJob())) {
return true;
}else {
return false;
}
}
public String show() {
retur