当前位置:编程学习 > JAVA >>

java 初学者遇到小问题,请前辈们赐教。

public class House implements Serializable { private String houseId; private String address; private int bedroomNo; protected double sellingPrice; //it used in the apartment private boolean sold; protected static double STAMPDUTY = 0.2; private Buyer buyer; /** * to sold a house */ public void sellHouse(Buyer buyer) { if (buyer != null) { if (!isSold()) { if (buyer.getPurchaseAmount() >= calculateFullPrice()) { System.out.println("The house can be sold at : " + calculateFullPrice() + " to the buyer" ); sold = true; this.buyer = buyer; } else { System.out.println("You are not afford to buy the property"); } } else { System.out.println("House already sold"); } } } /** * Constructor for objects of class House */ public House(String houseId, String address, int bedroomNo, double sellingPrice) { this.houseId = houseId; this.address = address; this.bedroomNo = bedroomNo; this.sellingPrice = sellingPrice; sold = false; } /** * Returns true when house is sold. */ public boolean isSold() { return sold; } 很抱歉,百度的问题有限制数量,我不能把所有的code都打出来。 这里不是已经有个 public boolean isSold().这个在这里干么呢?作用是什么的? 表达的意思有是什么呢?是把sold = false?还是什么呢?
答案:isSold()是一个返回boolean的方法,返回的是全局变量sold,sold在构造方法中被初始化为false。这个方法返回的值就是false;
其他:sold是house的一个属性,是判断house是否卖出。
而这个方法就是对于sold判断其值来表明house是否卖出。 这是常用存取器,也就是通常的get/set方法,就是来获得存取对象实例变量的值的,boolean类型的都是以“is..”命名方法。你这个就是用来获得实例变量sold的值的,含义就是判断是否卖出,初始是false也就是开始的时候默认为未售出。.在程序运行过程中或许sold会有变化。所以未必就是一定得到的sold=false. 

上一个:求助...java读取txt问题..
下一个:java源程序编译后如何发布制成磁盘文件

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,