编程题: 要求: 1).Person类有name,age,salary属性,要求实现至少两个构造方法,并且属性私有,提供对应的getter、setter。 2).覆写toString方法,要求在

编程题: 
要求: 
1).Person类有name,age,salary属性,要求实现至少两个构造方法,并且属性私有,提供对应的getter、setter。 
2).覆写toString方法,要求在System.out.println()函数中传递Person对象能打印出三个属性值而不是对象地址。 
3).覆写equals方法,要求两个Person类对象的值相同时返回true。 




package www.homework.java;


class Person1{
//name,age,salary
private String name;
private Integer age;
private double salary;




public Person1(String name, Integer age) {
this.name = name;
this.age = age;

}

public Person1(String name, Integer age, double salary) {
this.name = name;
this.age = age;
this.salary = salary;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}


@Override
public String toString() {

return "姓名"+name+"\t年龄"+age+"\t薪水"+salary;
}
public boolean equals(Object obj) {
         if (obj instanceof Person1) {
             Person1 person = (Person1) obj;
            return name.equals(person.getName());
         }
        return false;
    }


}


public class Homework {
public static void main(String[] args) {


Person1 per2=new Person1("宋典",21,10000);
Person1 per3=new Person1("宋典",22,10000);


System.out.println(per2);
System.out.println(per3);
System.out.println(per3.equals(per2));

}
}

你可能感兴趣的:(编程题: 要求: 1).Person类有name,age,salary属性,要求实现至少两个构造方法,并且属性私有,提供对应的getter、setter。 2).覆写toString方法,要求在)