有什么更好的方式与private field
和private methods
工作过他们吗?私人领域的公共方法
是它优美的使用私有方法的内部私有字段或最好是把他们作为参数,从公开的方法,当调用私有方法?
领域:
private Item model;
公共方法:
...
if (model.getPrice() != null) {
String formattedPrice = formatPrice();
}
...
私有方法:
private int formatPrice(){
int price = model.getPrice() + 10;
}
VS
2. 公共方法:
if (model.getPrice() != null) {
String formattedPrice = formatPrice(model.getPrice());
}
...
私有方法:
formatPrice(int price){
int price = price + 10;
}
如果你的'modifyPrice()'方法只能在这个类中使用(这似乎是因为'private'的可见性),我更喜欢第一个答案。 – sp00m 2012-04-24 13:00:31
感谢您的建议,我也使用第一,但犹豫 – sergionni 2012-04-24 13:02:14
@sergionni所以,不要犹豫;) – 2012-04-24 13:08:31