方法 knowsOop 可以被包内的任何人访问,因为 David 被定义.不要担心这两个访问修饰符,当您更多地了解 OOP 和 Java 时,它们会变得有意义.最后,你真的应该花时间阅读:http://java.sun.com/docs/书籍/教程/java/javaOO/index.html希望能帮到你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? 解决方案 When a method is public it means it can be accessed by other objects For instance:class David { // public method, can be use by anyone public String getName() { return "David"; } }The method getName may be accessed by other classes because it is public: class Other { David davidOne = new David(); String davidsName = davidOne.getName(); //<-- compiles and runs }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:class David { public String getName() { return "David"; } // private method... nobody but David's "instances" can use it.. private int getAge() { return 19; } }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: class Other { David davidOne = new David(); String davidsName = davidOne.getName(); int davidsAge = davidOne.getAge(); //<-- Compiler error, getAge() is not visible }But, if you can use it within David class:class David { public String getName() { return "David"; } // private method... nobody but David's "instance" can use it.. private int getAge() { return 19; } // Here the call to "getAge()" will succeed, because it is visible // inside the class public boolean hasSameAgeAs( David otherDavid ) { return this.getAge() == otherDavid.getAge(); } }The advantage? You can create a bunch of methods and keep them private, avoiding data corruption or in general preserving your objects encapsulated About encapsulationIn 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 ) class David { // protected method protected int getBalance() { return 1000000; } // package protected or "default" method boolean knowsOop(){ return true; } }There the method getBalance can only be accesed by David instances and David 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.htmlI hope this helps 这篇关于在java中将方法设为public/private/other是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 00:54