English 中文(简体)
为什么 get子和 set具的方法很重要? [复制]
原标题:Why are getter and setter method important in java? [duplicate]

我一直被教导到使用接收器和设计器。 然而,我不知道这些方法的利弊,也不知道如何执行这些方法,我们正在揭露数据并躲藏。

我对此感到困惑不解。 没有人能够就我们为什么使用一个跳板/走廊以及有什么好处提出适当建议?

最佳回答

基本“公共开端和定型的私人领域”在资本化方面确实是完全没有意义的,除非它使你有机会在不改变文体的情况下,在以后加以改变。

因此,不能不思议地利用这种模式。 仔细考虑你实际需要什么行动。

采集器和设计器的真正点是,只有在适当的情况下,才能使用这些装置,而且它们能够做的不只是去做和确定田地。

  • You can have only a getter. Then the property is read only. This should actually be the most common case.
  • You can have only a setter, making the property configurable, but communicating that nothing else should depend on its value
  • A getter can compute a value from several fields rather than return one field.
  • A getter can make a defensive copy
  • A getter can perform an expensive fetch operation lazily and use a field to cache the value
  • A setter can do sanity checks and throw IllegalArgumentException
  • A setter can notify listeners of changes to the value
  • You can have a setter that sets multiple fields together because they belong together conceptually. This doesn t adhere to the JavaBeans specification, so don t do it if you depend on frameworks or tools that expect JavaBeans. Otherwise, it s a useful option.

所有这些内容都是简单“简便”接口背后隐藏的执行细节。 这cap头指.。

问题回答

接收器和设计商的想法是控制某一类变量的获取。 这样,如果需要在国内改变价值,以不同的方式代表,那么你就可以这样做,而不必在课外违反任何法典。

例如,请允许我说,你有一个有距离变化的班子,它用ches来衡量。 几个月过去了,你在很多地方重新使用这一类别,你突然认识到你需要代表这一价值。 如果你不使用一台炉子和一台板块,那么你就不得不跟踪每一类物品的使用情况,并在那里转换。 如果你使用一个炉子和一个板块,那么你就只能改变使用该等舱的这些方法和一切。

public class Measurement
{

    /**
     * The distance in centimeters.
     */
    private double distance;

    /**
     * Gets the distance in inches.
     * @return A distance value.
     */
    public double getDistance()
    {
        return distance / 2.54;
    }

    /**
     * Sets the distance.
     * @param distance The distance, in inches.
     */
    public void setDistance(double distance)
    {
        this.distance = distance * 2.54;
    }
}

考虑的一个好处是,你不能为这一特定领域执行一套办法,就可以成为一个领域:ReadOnly

这里side倒。 汇款人/供应商往往向外部世界披露贵阶层的执行细节。 这不是一件好事。 你们正在撰写一套自动机械软件包。 因此,你需要一个汽车舱,因此,你暴露了田地的采集器和板块。

Date lastOilChangeDate;
int lastOilChangeMileage;

in this class. This is because the software wants to send emails when customer cars need oil changes.

但是,当新车出车时,如果你确定汽车是否需要与每3 000英里或3个月不同的石油变化? 也许这些新汽车在测量过脏的石油圈子里有传感器。 显然,你希望利用这一方法来确定是否需要石油变化。

The issue was you were solving the wrong problem with those getter/setters. No one really wants to know when the last oil change was, they want to know if you need another one. They were just implementation details, but you made them part of the interface. What you should have done was added a method

public boolean needsOilChange() 

而随后,卡勒班可以执行,但需要执行。 如果算法改变,则由于所有需要都是需要,机械学班将不得不放弃。 油库法。

There is nothing wrong with using getters and setters - just be aware that by using them and making them all public you re exposing your variables and, in a way, violating encapsulation. This is what the article you mention is trying to warn of - don t just automatically generate getters and setters for all private instance variables; think about what you want to expose to other classes (and at what level, i.e. private/protected/public) so that you re exposing only as needed.





相关问题
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 ...

热门标签