您如何将一个对象转换为一个double?

问题描述 投票:0回答:1

我需要能够获得存储在另一个类中的收入值,但是对于getter,它必须是public double getIncome()而不是public class getIncome。我不知道如何将值从对象转换为双精度型。我曾尝试寻找其他类似的问题,但是它们的代码过于混乱,因为它们对我来说尚属陌生,因此如果我理解的某些代码会有所帮助。

public class Income {

    private double income = 0;

    public Income() {
        super();
    }

    public double getIncome() {
        return income;
    }

    public void setIncome(double income) {
        this.income = income;
    }


public class Worker {

    private String name = null;
    private Income income;

    public Employee(String name, Income income) {
        super();
        this.name = name;
        this.income = income;
    }

    public double getIncome() {
        //This is the part I don't understand how to do
        return ;
    }

输出需要返回一个double而不是一个对象。感谢所有花时间回复的人。

java object double
1个回答
0
投票

仅从income对象返回Income属性:

public double getIncome() {
    return income.getIncome();
}

但是为什么需要将income包装在单独的对象中?您可以将其直接建模为double属性-除非您计划向Income类添加更多逻辑。

© www.soinside.com 2019 - 2024. All rights reserved.