我有以下代码:
public class Person
{
final String firstName;
final String lastName;
final int age;
final UUID identification;
public Person(final String firstName, final String lastName, final int age)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.identification = UUID.randomUUID();
}
protected Person(final String firstName, final String lastName, final int age, final UUID identification)
{
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.identification = identification;
}
/*
Getter functions
*/
public Person asPerson()
{
return this;
}
/*
Hash and Equals code
Equals checks for first/lastName, age, and identification
*/
}
public class Employee extends Person
{
final String occupation;
final float salary;
public Employee(final String firstName, final String lastName, final int age, final String occupation, final float salary)
{
super(firstName, lastName, age);
this.occupation = occupation;
this.salary = salary;
}
public Employee(final Person person, final String occupation, final float salary)
{
super(person.getFirstName(), person.getLastName, person.getAge(), person.getID());
this.occupation = occupation;
this.salary = salary;
}
/*
Getter functions for occupation and salary
*/
@Override
public Person asPerson()
{
return new Person(firstName, lastName, age, identification);
}
/*
Hash and Equals code
Equals checks for equality in occupation and salary
*/
}
public class Volunteer extends Person
{
final String location;
public Volunteer(final String firstName, final String lastName, final int age, final String location)
{
super(firstName, lastName, age);
this.location = location;
}
public Volunteer(final Person person, final String location)
{
super(person.getFirstName(), person.getLastName(), person.getAge(), person.getID());
this.location = location;
}
/*
Getter for location
*/
@Override
public Person asPerson()
{
return new Person(firstName, lastName, age, identification);
}
/*
Hash and Equals
Equals checks for equality in location.
*/
}
public Main
{
public static void main(String[] args)
{
final Person person = new Person("Man", "Fredman", 25);
final Person employee = new Employee(person, "Driver", 65000.0f);
final Person volunteer = new Volunteer(person, "Philly");
final boolean eqality = compareVtoE(volunteer, employee);
System.out.println(equality);
}
private boolean compareVtoE(final Person volunteer, final Person employee)
{
return volunteer.asPerson().equals(employee.asPerson());
}
}有一个已经定义的Employee变量,Employee中的asPerson函数是否可以返回超类实例而不必调用new Person(...)
我目前的工作是使用一个受保护的构造函数来接受identification,但是我认为有一个更好的方法来解决这个问题。
编辑
我扩大了这个例子。假设我有一个Volunteer和一个Employee,它们都扩展了Person,并且可以在构造函数中接受Person对象。他们可以是同一个人,但做不同的事情。要查看一名志愿者是否与员工相同,我需要一种获得Person对象的方法,而不需要更改UUID。我的工作是在Person中使用一个受保护的构造函数,它接受UUID,在带有super的子类构造函数中使用。我希望避免在asPerson()中使用构造函数,创建一个新的Person实例。
发布于 2018-12-07 23:48:40
我非常肯定,您无法按照您的要求进行更改,因为如果不更改对象,则需要更改对象,但以下是解决您的比较问题的两个可能的解决方案:
Person类中的Person方法,以便您可以比较不同角色的人员(员工或志愿者):
@覆盖公共布尔值等于(对象其他){如果(其他实例的人){返回identification.equals(((Person)other).identification);}返回false;}equals,例如需要在具有不同功能的派生类中重写它,只需创建如下isSamePerson函数:
公共布尔值isSamePerson(Person other) { if (其他!= null)返回identification.equals(other.identification);返回false;}这将为您节省不必要的对象复制,并有失去人员跟踪的危险。
https://stackoverflow.com/questions/53677377
复制相似问题