能够理解java中的setter和getter
我是java新手,但我喜欢这门语言,我想变得尽可能好,但我在“类”方面仍然遇到一些困难。 我了解什么是类以及什么是对象,但我仍然无法很好地理解 getters 和 setters
我知道类有
- 实例变量、
- 构造
- 函数、方法、
- setter 和 getters?
我不太明白为什么我需要它们以及我是否应该将它们写在构造函数中。
还想指出的是,我读过几篇互联网文章,但
- 那些文章太“技术性”了,
- 他们只是编写了没有实例变量、构造函数的代码,所以我不能很好地理解
即使您是初学者,也非常感谢您的帮助像我一样 ^^
编辑:
例如,我应该如何在这里使用 setter 和 getter?
class Demo {
int a=4;
Int b;
public Demo () {
System.println("a is <than b")
};
public int Demo (int b) {
if (a<b)
System.out.println("a is < than b");
else
System.out.println("b is < than a");
}
}
I'm new to java, but I like the language and I want to become as good as possible but I still have some difficulties with the "class" thing.
I understand what a class is and what objects are but still I cant understand well getters and setters
I know a class has
- Instance variables
- Constructors
- Methods
- Setters and getters ?
I can't understand well why do I need them and if I should write those inside a constructor.
Also like to point out that I've read several internet articles but
- those were to much "technical"
- they just write a code without instance variables, constructors so I can't understand very well
Your help is very appreciated even if you are a beginner like me ^^
Edit:
For example how i should use the setters and getters here?
class Demo {
int a=4;
Int b;
public Demo () {
System.println("a is <than b")
};
public int Demo (int b) {
if (a<b)
System.out.println("a is < than b");
else
System.out.println("b is < than a");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你要求一个简单的例子,所以我举一个。假设您正在设计一个圆圈类。圆可以通过其直径来表征:
调用者可能想知道圆的直径,因此您添加一个 getter:
您可能还想获取圆的面积,因此您为面积添加一个 getter:
然后您意识到对于圆的内部方法来说,使用半径而不是直径更容易。但是您希望保持班级的所有用户不变,以避免破坏大量现有代码。因此,您可以在不更改接口的情况下更改类的内部结构:
最后,您想更改圆的直径,因此您添加了一个设置器:
但是等等,直径为负的圆没有任何意义,因此您将方法更安全:
如果您在构造时预先计算了面积,则 setDiameter 也必须更改面积的值:
getter 和 setter 只是方法。但它们遵循命名约定,使您的代码易于掌握,并且可供许多依赖这些约定的框架使用。
You asked for a simple example, so I'll give one. Suppose you're designing a circle class. A circle can be characterized by its diameter:
A caller might want to know the diameter of the circle, so you add a getter:
You might also want to get the area of the circle, so you add a getter for the area:
And then you realize that using the radius rather than the diameter is easier for the internal methods of the circle. But you want to keep all the users of your class as is, to avoid breaking a lot of existing code. So you change the internals of the class without changing the interface:
And finally, you would like to change the diameter of the circle, so you add a setter:
But wait, a circle with a negative diameter makes no sense, so you make the method safer:
Had you precomputed the area at construction time, the setDiameter would have had to change the value of the area as well:
Getters and setters are just methods. But they follow a naming conventions that makes your code easy to grasp, and usable by a number of frameworks which rely on these conventions.
您需要它们来允许您的班级的用户获取和设置参数。这允许您的类跟踪参数何时设置,例如检查约束、强制一致性、更新其他值、使缓存无效。封装还允许您在版本之间修改类的内部表示,同时保持 API 兼容。
You need them to allow users of your class to get and set parameters. This allows your class to keep track of when parameters are set and e.g. check constraints, enforce consistency, update other values, invalidate caches. The encapsulation also allows you to modify the internal representation of the class between versions while keeping the API compatible.
getter 或 setter 只是返回类属性或设置类属性的函数。使用它们的原因是因为通常类属性被定义为
private
,这意味着您只能在类方法中直接引用它们。getter 是一个返回私有值的
public
方法(意味着可以从类本身的外部调用它)。这允许该属性保持私有,但仍然可以在类之外访问。setter 是一个设置私有值的
public
方法。正如您可能已经猜到的,这允许该属性保持私有状态,但仍然可以在类之外设置。A getter or setter is just a function that returns a class property, or sets a class property. The reason you use them is because typically class properties are defined as
private
, which means you can only reference them directly within class methods.A getter is a
public
method (meaning it can be called from outside of the class itself) that returns a private value. This allows the property to remain private but still be accessible outside of the class.A setter is a
public
method that sets a private value. As you may have guessed, this allows the property to remain private but still be set outside of the class.Setter 和 getter 只是用于访问与类关联的字段的方法:
Setters and getters are just methods that are used to access fields associated with a class:
getter 和 setter 是访问 Java Bean 与类关联的属性。这只是能够访问类有兴趣共享的公共字段的标准方法。它们由 Java Bean 规范定义。
The getters and setters are a way of accessing Java Bean properties associated with a class. It's just a standard means of being able to access the public fields that the class is interested in sharing. They are defined by the Java Bean specification.