Java 中的设置方法
有人可以解释一下如何使用 set 方法吗?问题:
class Sonum {
private int prior;
public Sonum(int prior) {
this.prior = prior;
}
public int getPrior() {
return prior;
}
public void setPrior(int prior) {
this.prior = prior;
}
class Tel {
// Please explain how can I set the value for prior? (for example 2)
}
Could anubody explain how to use set methods? Problem:
class Sonum {
private int prior;
public Sonum(int prior) {
this.prior = prior;
}
public int getPrior() {
return prior;
}
public void setPrior(int prior) {
this.prior = prior;
}
class Tel {
// Please explain how can I set the value for prior? (for example 2)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
好吧,首先您需要一个要在其上设置
prior
值的Sonum
实例。例如:Well, first you need an instance of
Sonum
on which you want to set theprior
value. For example:深吸一口气。 Java 教程。阅读它。你会明白的。
Take a deep breath. The Java Tutorial. Read it. You will understand.
请参阅
创建对象和使用对象
http://download.oracle.com/javase/tutorial/java /javaOO/objectcreation.html
Refer
Creating Objects & Using Objects
http://download.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
“Setter 方法”并不是魔法。它们只是常规方法。您需要该类的一个实例,然后可以调用它的方法。就像任何其他 Java 对象一样。
"Setter methods" aren't magic. They're just regular methods. You need an instance of that class, and then you can call the methods on it. Just like any other Java object.
set方法处理一个私有值,我们希望阻止他直接使用我们的客户端方式,因此有get\set方法。
get\set方法最大的优点就是控制能力!
例如,当我们想要设置年龄时,我们可以控制最低年龄,以及许多其他简单的示例。
示例:
现在我想您可以轻松理解这个硬件了:)
set method deal with a private value that we would like to prevent the direct way to him using our client, therefor there are get \ set method.
The biggest advantage of get \ set methods is the control ability !
We can for example control a minimum age when we want to set an age, and many other simple examples.
Example:
Now I think you can easily understand this HW :)