java中方法的public/private/other是什么意思?
java中方法的public/private/other是什么意思?
这些选项的优点和缺点是什么?
作为一个想成为一名优秀程序员的人,我关心的动力是什么?
What does it mean for a method to be public/private/other in java?
What are the advantages and disadvantages of these options?
What is my impetus, as someone trying to be a good programmer, to care?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当一个方法是public时,意味着它可以被其他对象访问
例如:
方法
getName
可以被其他类访问,因为它是public:优点..您可以从其他地方使用它。
当一个方法是私有时,意味着它只能由同一类的对象访问。
例如,在这个新定义中:
方法
getAge
不能被其他类访问,因为它是私有的,如果你尝试这样做,编译器会给你一条错误消息:但是,如果你可以在 within David 类中使用它:
优势是什么?您可以创建一堆方法并保持它们私有,避免数据损坏或一般保留您的对象封装< /a>
关于封装
在OOP(面向对象编程)中目的是根据现实生活中的对象对软件进行建模。
现实生活中的对象具有(除其他外)属性和访问这些属性的方法。
您希望将其中一些方法公开,而将其他方法保密。
例如,一个人类存在,有一颗心。但它并不是暴露给所有人的,否则会很危险。它封装在我们的体内。
如果我们要根据真实的人类来建模软件,我们可以将方法:
heartBeat
声明为私有(因此,没有人可以访问它),另一方面,它会是使用 getGender 等公共方法来确定您的Human实例是男性还是女性非常有用。
还有其他访问修饰符,例如:“protected”和 package protected(没有关键字)
其中方法
getBalance
只能由David
实例访问,并且 < code>David 子类(为子类创建另一个线程)方法
knowsOop
可以由定义 David 的包内的任何人访问。不用担心这两个访问修饰符,当您了解更多有关 OOP 和 Java 的知识时,它们会有意义。
最后,您真的应该花时间阅读:
http:// java.sun.com/docs/books/tutorial/java/javaOO/index.html
我希望这有帮助
When a method is public it means it can be accessed by other objects
For instance:
The method
getName
may be accessed by other classes because it is public:The advantage.. well you can use it from other places.
When a method is private it means it can only be accessed by objects OF THE SAME CLASS
For instance, in this new definition:
The method
getAge
can't be accessed by other classes because it is private, if you try to do it, the compiler will give you an error message:But, if you can use it within David class:
The advantage? You can create a bunch of methods and keep them private, avoiding data corruption or in general preserving your objects encapsulated
About encapsulation
In OOP ( object oriented programming ) the intention is to model the software after real life objects.
Real life objects have ( among other things ) attributes and methods to access those attributes.
You want to make public some of those methods, and keep private others.
For instance, a Human being, have a heart. But it is not exposed to everybody, it would be dangerous. It is encapsulated inside our body.
If we were to model a software after a real Human we may declare the method:
heartBeat
as private ( so, nobody can access it )In the other hand, it would be useful to have come public methods like
getGender
to find out if your Human instance is male or female.There are other access modifiers such as: "protected" and package protected ( whose doesn't have a keyword )
There the method
getBalance
can only be accesed byDavid
instances andDavid
subclasses ( create another thread for what is a subclass )The method
knowsOop
can be accesses by anyone inside the package as David is defined.Don't worry about this two access modifiers, they will make sense when you learn more about OOP and Java.
Finally you should really, really take time to read:
http://java.sun.com/docs/books/tutorial/java/javaOO/index.html
I hope this helps
公共方法可以从任何地方访问,私有方法只能从同一个类访问。主要优点是对类的 API 的控制。如果我只公开需要的内容,我可以更改类的内部行为,而不会破坏依赖于该类的代码。你应该关心,因为软件在现实世界中经常发生变化(至少这是我的经验,其他人也有这样的经历),并且每次变化越多,你需要投入更多的精力进行维护,或者你的软件有更多的错误。归根结底还是成本问题。
向此类的用户隐藏类的内部结构以避免以后的更改破坏代码的可能性通常称为封装或 信息隐藏。
除了 public 和 private 之外的两个选项是 package(不带修饰符)和 protected。包可访问的方法也可以从同一包的类内访问。我不记得以任何有用的方式使用过该选项。可以从继承相关类的类访问受保护的方法。这通常用于为基类的已定义 API 创建具有具体行为的类。例如,您可以通过扩展 来实现新的列表类吗AbstractList 并且您只需要实现 get 和 size (以及用于可修改列表的一种 set 方法)。 List API 公开的其他方法在基类中定义,如果需要则调用其他三个方法。
A public method can be accessed from everywhere, a private method only from the same class. The main advantage is the control over the API of an class. If I make only public what is needed, I can change the internal behaviour of a class , without breaking code depending on this class. You should care, because software changes often in the real world (at least it's my experience and others have it too) and the more every change breaks, the more energy you have to put into maintenance or the more bugs your software has. In the end it's a question of costs.
The possibility to hide internals of your class from users of this class to avoid breaking code by later changes is often called encapsulation or information hiding.
The two options besides public and private are package (without an modifier) and protected. The package-accessible method can also be accessed from within classes of the same package. I cannot remember to used that option in any useful way. protected methods can be accessed from classes, that inherit the class in question. That is often used to create classes with concrete behaviour for a defined API of the base-class. For example could you implement a new List-class by extending AbstractList and you only need to implement get and size (and one set-method for modifiable lists). The other methods exposed by the API of List are defined in the base-class, calling the three other methods if needed.
私有方法只能在类内部调用。您可以在程序中的任何位置调用类的公共方法。没有访问修饰符的方法意味着具有包可见性范围(称为默认),因此您可以在定义类的包中的任何位置调用它。
请参阅http://en.wikipedia.org/wiki/Object_orient_programming#Encapsulation
Private methods can be called only inside the class. You can call public methods of your class anywhere in program. Methods without access modifier are meant to have package visibility scope (it's called default), so you can invoke it anywhere in package, where class is defined.
See http://en.wikipedia.org/wiki/Object_oriented_programming#Encapsulation
Hpublic、protected 和 private 修饰符控制哪些其他代码可以看到这些方法(或字段)。这是关于控制您公开的界面。
常用的有:
public 修饰符:任何其他人都可以看到你的方法。
private 修饰符:除了您的类和任何内部类之外,没有任何代码可以看到您的方法。
例如,如果您想确保只创建了一个类的单个实例(singleton模式)。您可以将构造函数设为私有,创建一个实例并将其存储为称为实例的私有成员,并提供一个类似这样的公共方法:
这样您就可以保证只有一个实例。
更新 - 根据要求的另一个示例。
另一个例子可能是您有一个复杂的公共方法,并且您希望将其分解为更简单的部分。您可以将其分解为更简单的方法,每个方法执行部分工作,但您不希望其他代码调用这些部分方法,因为它们无法单独工作 - 因此您可以将较小的方法设为私有,确保不能在班级之外调用它们。
HThe public, protected and private modifiers control what other code can see those methods (or fields). It's about controlling the interface you're exposing.
The commonly useful ones are:
The public modifier: any other can see your method.
The private modifier: no code other than your class and any inner classes can see your method.
These would be useful for example if you wanted to ensure there was only a single instance of a class ever created (singleton pattern). You could make the constructor private, create a single instance and store is as a private member called instance, and provide a public method something like this:
and so you can guarantee that there will only every be one instance.
Update - another example as requested.
Another example might be where you have a complicated public method and you want to break it down into simpler parts. You could break it down into simplr methods, each doing part of the job, but you wouldn't want other code to call those part methods, as they wouldn't work on their own - so you would make the smaller methods private, ensuring that they can't be called outside your class.
主要原因称为封装:不允许访问对象的内部状态。
the main reason is called encapsulation: don't give access to internal state of object.
首先,我会开始尽可能地限制访问。从
私有
开始。如果您碰巧需要其他地方的构造函数、方法或字段,但由于限制而无法访问它,那么接下来的步骤就是问自己:第 1 点避免了错误耦合,第 2 点改进了封装。一旦您考虑了上述情况并得出结论认为确实需要减少限制,那么请将其设置为进一步开放一步或更多。
For starters, I would to start restrict the access as much as possible. Start with
private
. If you happen to need the constructor, method or field from somewhere else, but cannot access it due to the restriction, then the next steps would be to ask yourself:Point 1 avoids wrong coupling and point 2 improves encapsulation. Once you've considered the above and concluded that less restriction is really needed, then set it one step or more further open.