定义一个Employee类继承于Person类,包含新的属性:职位;写好相关的方法,能够构造的时候初始化或者后续修改属性值,重写equals方法,比较规则:当两个对象的姓名,年龄,性别,职位都相等时返回true,否则返回false。
/*
*写一个测试类进行相关测试(要求可以打印对象的信息,能够进行对象的比较是否"相等")。
*/
public class EmployeeTestDemo {
public static void main(String[] args){
Employee employee1=new Employee("Jim",28,'男',"软件工程师");
Employee employee2=new Employee("Tom",28,'男',"软件工程师");
employee1.getDetail();
employee2.getDetail();
System.out.println("判断两位职员是否“相等”:");
System.out.println(employee1.equals(employee2));
}
}
public class Person {
private String name;//姓名
private int age;//年龄
private char sex;//性别
public Person(String name,int age,char sex){
this.setName(name);
this.setAge(age);
this.setSex(sex);
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void setSex(char sex) {
this.sex = sex;
}
public char getSex() {
return sex;
}
}
public class Employee extends Person{
private String position;//职位
public Employee(String name,int age,char sex,String position){
super(name, age, sex);
this.setPosition(position);
}
public void setPosition(String position) {
this.position = position;
}
public String getPosition() {
return position;
}
//重写方法,判断对象是否相等
public boolean equals(Employee b){
if(this.getName()==b.getName()&&this.getAge()==b.getAge()
&&this.getSex()==b.getSex()&&this.getPosition()==b.getPosition()){
return true;
}
else{
return false;
}
}
//输出对象的信息
public void getDetail(){
System.out.println("name: "+this.getName()+"age: "+this.getAge()+"sex: "+this.getSex()+"position: "+this.getPosition());
}
}