如何在Java中使用私有方法
我得到一个具有私有方法 setCoors(int x, int y) 的类。该类的构造函数中也包含 setCoors。在另一个类中,我想要一个调用 setCoors 的方法 setLocation。这可能吗?
新问题:
如果我不允许将该方法设置为公共,这可能吗?
public class Coordinate{
public Coordinate(int a, int b){
setCoors(a,b)
}
private void setCoords(int x, int y)
}
public class Location{
private Coordinate loc;
public void setLocation(int a, int b)
loc = new Coordinate(a,b)
}
I am given a class that has a private method say setCoors(int x, int y). The constructor of that class has the setCoors in it too. In a different class, I want to have a method setLocation which calls setCoors. Is this possible?
New Question:
If I am not allowed to set the method to public, is this possible?
public class Coordinate{
public Coordinate(int a, int b){
setCoors(a,b)
}
private void setCoords(int x, int y)
}
public class Location{
private Coordinate loc;
public void setLocation(int a, int b)
loc = new Coordinate(a,b)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,
private
意味着该方法只能在定义它的类内部调用。您可能希望让setLocation
创建setCoords
所在类的新实例,或者更改setCoords
的可见性。编辑:您发布的代码将起作用。请注意,
Location
类的任何实例都将绑定到其自己的Cooperative
对象。如果您在代码中的其他位置创建一个新的坐标
对象,您将无法修改其内部状态。换句话说,该行将创建对象
myCoord
,该对象将永远具有坐标4
和5
。No,
private
means the method can only be called inside of the Class in which it is defined. You will probably want to havesetLocation
create a new instance of the classsetCoords
resides in, or change the visibility onsetCoords
.EDIT: The code you have posted will work. Just be aware that any instance of the
Location
class will be bound to its ownCoordinate
object. If you create a newCoordinate
object somewhere else in your code, you will be unable to modify its internal state. In other words, the linewill create the object
myCoord
which will forever have the coordinates4
and5
.最好和最有帮助的答案取决于问题的上下文,我认为这并不完全显而易见。
如果问题是关于 private 的预期含义的新手问题,那么答案“否”是完全合适的。即:
现在,如果,好吧,也许这是一个延伸(谢谢布莱恩:)),这个问题来自一个更“高级”的上下文,人们正在考虑“我知道私有意味着private 但是有语言漏洞吗”,那么,嗯,有这样的漏洞。它是这样的:
输出:
当然,这确实不是应用程序程序员会做的事情。但事实上,这样的事情可以做到是值得知道的,而不是应该被忽视的事情。无论如何,恕我直言。
The best and most helpful answer depends on the context of the question, which is, I believe, not completely obvious.
If the question was a novice question about the intended meaning of private, then the answer "no" is completely appropriate. That is:
Now, if, and okay maybe this is a stretch (thank you Brian :) ), that the question came from a more "advanced" context where one is looking at the question of "I know private means private but is there a language loophole", then, well, there is such a loophole. It goes like this:
Output:
Of course, this really isn't something that application programmers would ever do. But the fact that such a thing can be done is worthwhile to know, and not something that should be ignored. IMHO anyway.
private
表示它是 private如果您希望其他类调用它,也许您不应该将其设为私有?
private
means it's privateIf you want other classes to call it, maybe you shouldn't make it private?
没有
private
方法不能在定义它们的类之外访问No
private
methods can't be accessed outside the class in which they are defined孩子做作业:答案是否定的。一个人需要一些疯狂的工作来完成他的工作:答案是肯定的。更重要的是,您的
setCoors
方法不应采用 int 参数。它应该需要两个SilverBullet
对象。Kid-doing-homework: the answer is no. Guy-requiring-some-crazy-work-around-for-his-job: the answer is yes. Far more importantly though, Your
setCoors
method should not take int arguments. It should take twoSilverBullet
objects.private
表示您只能在定义的类内部访问它。private
means you can only access it inside the class defined.