24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 708  |  回复: 5

2012200838

实习版主

优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!

[求助] 去掉类中的设置方法,类是可以不可改变的吗?

问题:如果重新定义程序清单的Loan类,去掉其中的设置方法,这个类是不可改变的吗?
If you redefine the Loan class without setter methods, is the class immutable?
这是原书的标准答案:
No. It must also contain no get methods that would return a reference to a mutable data field object.

不是很理解,get方法不是仅仅是访问吗,而且数据域是私有的,怎么是可以改变的的呢?

代码:
CODE:
public class Loan {
  private double annualInterestRate;
  private int numberOfYears;
  private double loanAmount;
  private java.util.Date loanDate;

  /** Default constructor */
  public Loan() {
    this(2.5, 1, 1000);
  }

  /** Construct a loan with specified annual interest rate,
      number of years and loan amount
    */
  public Loan(double annualInterestRate, int numberOfYears,
      double loanAmount) {
    this.annualInterestRate = annualInterestRate;
    this.numberOfYears = numberOfYears;
    this.loanAmount = loanAmount;
    loanDate = new java.util.Date();
  }

  /** Return annualInterestRate */
  public double getAnnualInterestRate() {
    return annualInterestRate;
  }

  /** Set a new annualInterestRate */
  public void setAnnualInterestRate(double annualInterestRate) {
    this.annualInterestRate = annualInterestRate;
  }

  /** Return numberOfYears */
  public int getNumberOfYears() {
    return numberOfYears;
  }

  /** Set a new numberOfYears */
  public void setNumberOfYears(int numberOfYears) {
    this.numberOfYears = numberOfYears;
  }

  /** Return loanAmount */
  public double getLoanAmount() {
    return loanAmount;
  }

  /** Set a newloanAmount */
  public void setLoanAmount(double loanAmount) {
    this.loanAmount = loanAmount;
  }

  /** Find monthly payment */
  public double getMonthlyPayment() {
    double monthlyInterestRate = annualInterestRate / 1200;
    double monthlyPayment = loanAmount * monthlyInterestRate / (1 -
      (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
    return monthlyPayment;   
  }

  /** Find total payment */
  public double getTotalPayment() {
    double totalPayment = getMonthlyPayment() * numberOfYears * 12;
    return totalPayment;   
  }

  /** Return loan date */
  public java.util.Date getLoanDate() {
    return loanDate;
  }
}

回复此楼
生命就是一场旅行,不要错过了路边的风景!
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

2012200838

兑换贵宾

优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!

生命就是一场旅行,不要错过了路边的风景!
2楼2015-11-14 14:42:29
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

mfkkgriu

主管区长

优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!

java啊,应该差不多没有set设置方法好像是不能改变吧。技术不精说错勿怪。

发自小木虫Android客户端
3楼2015-11-14 15:33:53
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

mfkkgriu

超级版主

优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!

汗,忘记了函数返回值可以改变。>_<

发自小木虫Android客户端
4楼2015-11-14 15:35:35
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

2012200838

实习版主

优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!

引用回帖:
3楼: Originally posted by mfkkgriu at 2015-11-14 15:33:53
java啊,应该差不多没有set设置方法好像是不能改变吧。技术不精说错勿怪。

其实这个答案贴错了,不过这个答案也指出问题所在。

如果A类私有数据域data 数据类型为B类的对象的引用,难道可以在A类外通过A类中get方法调用data用B类中set方法改变data.

不知道我表达清楚没有。

书上关于类不可改变的条件是这样说的:
For a class to be immutable, it must meet the following requirements:
1.All data fields must be private.
2.There cant be any mutator methods for data fields.
3.No accessor methods can return a reference to a data field that is mutable.

You should take the 3rd requirement seriously.

发自小木虫Android客户端
生命就是一场旅行,不要错过了路边的风景!
5楼2015-11-14 18:01:42
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

2012200838

主管区长

优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!优秀!!有木有!!!

引用回帖:
3楼: Originally posted by mfkkgriu at 2015-11-14 15:33:53
java啊,应该差不多没有set设置方法好像是不能改变吧。技术不精说错勿怪。

其中loanDate 可以改变。

发自小木虫Android客户端
生命就是一场旅行,不要错过了路边的风景!
6楼2015-11-14 18:03:42
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 2012200838 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 一志愿北化085600材料专硕275|有文章专利|求调剂 +16 Micky11223 2026-03-25 17/850 2026-03-31 22:04 by 544594351
[考研] 已决定调剂院校 +8 JKSOIID 2026-03-26 8/400 2026-03-31 19:51 by mg1014
[考研] 一志愿华东理工大学,080500学硕,317分,求调剂 +9 s1145 2026-03-31 9/450 2026-03-31 18:47 by JourneyLucky
[考研] 08开头看过来!!! +3 wwwwffffff 2026-03-31 5/250 2026-03-31 17:45 by 星光/
[考研] 318求调剂 +10 陈晨79 2026-03-30 10/500 2026-03-31 17:37 by 544594351
[考研] 机械学硕总分317求调剂!!!! +6 Acaciad 2026-03-25 6/300 2026-03-31 16:52 by asdfzly
[考研] 求化学调剂 +12 wulanna 2026-03-28 12/600 2026-03-31 16:38 by 690616278
[考研] 材料专硕调剂 +13 椰椰。 2026-03-29 13/650 2026-03-31 16:37 by hypershenger
[考研] 化学0703 调剂 306分 一志愿211 +10 26要上岸 2026-03-28 10/500 2026-03-31 16:04 by 记事本2026
[考研] 本科211生物医学工程085409求调剂339分 +7 里子木yy 2026-03-29 7/350 2026-03-31 14:35 by fmesaito
[考研] 313求调剂 +6 卖个关子吧 2026-03-31 6/300 2026-03-31 10:58 by Jaylen.
[考研] 一志愿中海洋320化学工程与技术学硕求调剂 +8 披星河 2026-03-30 8/400 2026-03-31 08:53 by lbsjt
[考研] 327求调剂 +5 小卡不卡. 2026-03-29 5/250 2026-03-30 19:30 by Wang200018
[考研] 297求调剂 +17 田洪有 2026-03-26 18/900 2026-03-30 18:32 by nothing投稿中
[考研] 342求调剂 +4 加油a李zs 2026-03-26 4/200 2026-03-30 16:39 by 晶体之美
[考研] 356求调剂 +3 gysy?s?a 2026-03-28 3/150 2026-03-29 00:33 by 544594351
[考研] 070300求调剂306分 +4 26要上岸 2026-03-27 4/200 2026-03-28 13:06 by 唐沐儿
[考研] 0703化学求调剂,各位老师看看我!!! +5 祁祺祺 2026-03-25 5/250 2026-03-27 21:44 by 东方猪猪
[考研] 调剂 +4 柚柚yoyo 2026-03-26 4/200 2026-03-26 20:43 by fmesaito
[考研] 347求调剂 +4 L when 2026-03-25 4/200 2026-03-25 13:37 by cocolv
信息提示
请填处理意见