在java中将方法设为public/private/other是什么意思? [英] What does it mean for a method to be public/private/other in java?

查看:16
本文介绍了在java中将方法设为public/private/other是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 中方法为 public/private/other 是什么意思?
这些选项的优缺点是什么?
作为一个想成为一名优秀程序员的人,我关心的动力是什么?

解决方案

当一个方法是public时,意味着它可以被其他对象访问

例如:

类大卫{//公共方法,任何人都可以使用公共字符串 getName() {返回大卫";}}

方法 getName 可以被其他类访问,因为它是公共的:

 class 其他 {David davidOne = 新大卫();String davidsName = davidOne.getName();//<-- 编译并运行}

优点..你可以从其他地方使用它.

当一个方法是私有时,这意味着它只能被相同类的对象访问

例如,在这个新定义中:

类大卫{公共字符串 getName() {返回大卫";}//私有方法......除了大卫的实例"之外没有人可以使用它......私人 int getAge() {返回 19;}}

方法getAge不能被其他类访问,因为它是私有的,如果你尝试这样做,编译器会给你一个错误信息:

 class 其他 {David davidOne = 新大卫();String davidsName = davidOne.getName();int davidsAge = davidOne.getAge();//<-- 编译错误,getAge() 不可见}

但是,如果您可以大卫类中使用它:

类大卫{公共字符串 getName() {返回大卫";}//私有方法......除了大卫的实例"之外没有人可以使用它......私人 int getAge() {返回 19;}//这里对getAge()"的调用会成功,因为它是可见的//在类内部公共布尔hasSameAgeAs(大卫其他大卫){返回 this.getAge() == otherDavid.getAge();}}

优势?您可以创建一堆方法并将它们保密,避免数据损坏或通常保留您的对象 encapsulated

关于封装

OOP(面向对象编程)中的意图是在真实生活对象.

现实生活中的对象具有(除其他外)属性和访问这些属性的方法.

您希望公开其中一些方法,而将其他方法保密.

例如,一个人类,有一颗心.但它不是暴露给每个人的,它会很危险.它封装在我们体内.

如果我们要模仿一个真正的人类来建模一个软件,我们可以将方法:heartBeat 声明为私有的(因此,没有人可以访问它)

另一方面,使用getGender之类的public方法来确定您的Human实例是男性还是女性.

还有其他访问修饰符,例如:"protected" 和 package protected(没有关键字)

 class David {//受保护的方法受保护的 int getBalance() {返回 1000000;}//包保护或默认"方法布尔知道Oop(){返回真;}}

getBalance 方法只能被 David 实例和 David 子类访问(为子类创建另一个线程)

方法 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 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 )

 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.html

I hope this helps

这篇关于在java中将方法设为public/private/other是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆