![](/img/trans.png)
[英]Java, is it possible to 'convert' object from subclass to object from superclass
[英]Is it possible to return superclass object from initialized subclass without initializing superclass
我有以下代碼:
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
已經定義的變量,是那里的一個方式asPerson
在功能上Employee
,而不必調用返回超類的實例new Person(...)
我當前的解決方法是使用受保護的構造函數進行identification
,但是我認為有更好的方法來解決此問題。
編輯
我已經擴展了示例。 假設我有一個Volunteer
和Employee
,它們都擴展了Person
並可以在構造函數中使用Person
對象。 他們可以是同一個人,但做不同的事情。 要查看一個志願者是否與雇員相同,我需要一種無需更改UUID即可獲取Person
對象的方法。 我的解決方法是在Person
中使用受保護的構造函數,該構造函數接受UUID,並在super
的子類構造函數中使用它。 我想避免在asPerson()
使用構造函數,而創建Person
的新實例。
我非常確定沒有辦法完成您要的操作,因為這需要更改對象而不更改它,但是這是比較問題的兩種可能的解決方案:
覆蓋Person
類中的equals
方法,以便您可以比較人員(無論其角色是雇員還是志願者):
@Override public boolean equals(Object other) { if (other instanceof Person) { return identification.equals(((Person)other).identification); } return false; }
如果由於某種原因不能使用equals
,例如,您需要在具有不同功能的派生類中重寫equals
,只需創建一個isSamePerson
函數,如下所示:
public boolean isSamePerson(Person other) { if (other != null) return identification.equals(other.identification); return false; }
這將節省您不必要的對象重復,並避免了疏散人員的危險。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.