English 中文(简体)
私人领域
原标题:Private fields inside private methods VS pass them from public methods

如何更好地与<代码> 私人领域和<代码> 私人方法合作?

是否在私人方法中使用私人领域,或者在使用私人方法时将其作为参数更好?

field:

private Item model;

1. public method:

...
if (model.getPrice() != null) {
 String formattedPrice = formatPrice();
}
...

私人方法:

private int formatPrice(){
 int price =  model.getPrice() + 10;
}

http://www.ohchr.org。

2. public method:

if (model.getPrice() != null) {
 String formattedPrice = formatPrice(model.getPrice());
}
...

私人方法:

formatPrice(int price){
 int price = price + 10;
}
最佳回答

是否在私人方法中使用私人领域,或者在使用私人方法时将其作为参数更好?

我认为,这是非常主观的,取决于你们的偏好,也取决于具体情况。

In general, both approaches seem valid to me. There s no problem accessing a private member from a private method, and there s no problem passing it as a parameter.

Regarding the specific example, I slightly prefer the 2nd implementation. The private method is self-contained there – it doesn t depend on anything other than its parameters. This makes it easier to read, test, and reuse. You can reason about the method and argue about its correctness without needing to know what model is and how its getPrice method works.

问题回答

在这方面,我可以指出:

......

    private int formatPrice() {
        // format model.getPrice()
    }

... potentially avoids the "problem" of formatting the wrong thing as a price, and potentially saves a few characters if you call it in lots of places.

......

    private int formatPrice(int price) {
        // format price
    }

......可以用来规范来自不同来源的价格(而不是从<>条码>中提取的>标准/代码”,但这种呼吁只是一个比喻,而且没有什么东西可以阻止你将某些不代表<>/m>价格的分类。


However, in my opinion, there s not much difference between the two approaches. Certainly, not enough difference for one or the other be clearly a "better way" in general.

You should use private methods when you don t want them to be seen outside of the class you re implementing. So if one would create an instance of that class, that private method would be inaccessible than.

As for private fields, same logic. Well nothing is really private in Java anyways, you can get to it by using reflection anyways..





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签