我需要能够获得存储在另一个类中的收入值,但是对于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而不是一个对象。感谢所有花时间回复的人。
仅从income
对象返回Income
属性:
public double getIncome() {
return income.getIncome();
}
但是为什么需要将income
包装在单独的对象中?您可以将其直接建模为double
属性-除非您计划向Income
类添加更多逻辑。