首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否可以从初始化的子类返回超类对象而不初始化超类?

是否可以从初始化的子类返回超类对象而不初始化超类?
EN

Stack Overflow用户
提问于 2018-12-07 22:05:36
回答 1查看 52关注 0票数 0

我有以下代码:

代码语言:javascript
运行
复制
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实例。

EN

Stack Overflow用户

发布于 2018-12-07 23:48:40

我非常肯定,您无法按照您的要求进行更改,因为如果不更改对象,则需要更改对象,但以下是解决您的比较问题的两个可能的解决方案:

  1. 重写Person类中的Person方法,以便您可以比较不同角色的人员(员工或志愿者): @覆盖公共布尔值等于(对象其他){如果(其他实例的人){返回identification.equals(((Person)other).identification);}返回false;}
  2. 如果由于某些原因不能使用equals,例如需要在具有不同功能的派生类中重写它,只需创建如下isSamePerson函数: 公共布尔值isSamePerson(Person other) { if (其他!= null)返回identification.equals(other.identification);返回false;}

这将为您节省不必要的对象复制,并有失去人员跟踪的危险。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53677377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档