English 中文(简体)
私人和公共java获得修饰基本基本知识
原标题:Private and public java access modifier basics
  • 时间:2012-05-24 19:44:50
  •  标签:
  • java

我对Java来说比较新,我读过一些私人和公共的优胜修饰者,我想一劳永逸地消除这个议题上的任何混乱,因为我觉得我对此没有最好的了解。

变量 < / 强 > 中的 < 强 > 访问修正器

如果我错了,请纠正我,一个变量在默认情况下是公开的。如果访问修改器设置为公开的,或者只是完全没有设置,则与同一工程的其他类不同,可以访问整数和(或)修改它。如果它是私密的,而不是外部可见的,并且无法被创建它所在的类别以外的类访问。

< 强度> 班级中的 < 强度 > 访问修饰器

在班级中访问修饰者,我似乎并不完全理解。据我的理解,如果我从非同一类的方法中叫私人方法,而不是用它不会起作用。如果它比它公开,那么它是行不通的?

有什么我漏掉的还是没正确理解的?

我感谢在这方面的帮助。

最佳回答

如果我错了,请纠正我,一个变数在违约时是公开的。

您错了。 假设您指的是字段, 默认情况下, 它们有“ 包件” 访问, 无法明确表达 。 < em> 本地 < / em > 变量没有访问控制的概念 - 它们只存在于一个方法的范围内, 所以无法在其他地方被引用 。

如果访问修饰器被设置在公共场合,或只是完全没有设置,则与同一项目的其他类别不同,可访问整数和/或修改整数。

如果它公开, 任何 代码都可以访问它。

如果默认访问( 包件), 同一包件中的任何代码都可以访问它 。

在班级中访问修饰者,我似乎并不完全理解。据我的理解,如果我从非同一类的方法中叫私人方法,而不是用它不会起作用。如果它比它公开,那么它是行不通的?

没错,没错,是的。

我建议你阅读"http://docs.oracle.com/javase/tuative/java/javaoo/accesscontrol.html" rel=“no follow”>>Java辅导课程,并查阅http://docs.oracle.com/javase/specs/jls/se7/html/jls/jls-6.html#jls-6" rel=“nofolfollow”第6.6 节,以了解更多细节。

问题回答

http://docs.oracle.com/javase/tutimental/java/javaOO/ access control.html" rel="nofollow"。 有关这个主题的一个很好的博士。

有四个准入等级:

  1. private: Only that class (not even descendants) can access.
  2. protected: Only that class and its descendants and classes in the same package can access.
  3. package-private (no specifier)--only classes in the same package can access--even subclasses that are not in the same package cannot.
  4. public: Everything can access.

对于两类成员变量和方法,默认访问都是私有的。

您可以在甲骨文网站找到有关4个修饰者"http://docs.oracle.com/javase/tutiment/java/javaOO/accesscontrol.html" rel=“no follow”>这里

如果您不指定修饰符, 则默认为默认, 这意味着只有同一软件包中的任何代码才能访问它 。

如果它公开,那么任何软件包中的任何代码都可以访问它。

实际上,在爪哇,实际上有四种不同的访问修饰器,私人的、公共的、受保护的和包包的。“如果我错了,请纠正我,一个变量默认是公开的”—如果不是以不同的方式宣布,这里是错的。如果它被设置为公开的,那么它也可以被其他类别修改,即使是原始包之外的其他类别。这个网站—http://javapapers.com/core-java/access-modacters-in-java-explain/"rel=“nofolpol”> >http://javapapers.com/core-java/access-modors-in-java-explain/ - 可以为您提供有用的提示。事实上,只要您阅读更多一些O概念,例如Encapsulation, 就会很容易理解访问修饰器的目的(以确保数据整) 。

Java提供一些访问修改器,以设定类别、变量、方法和构造器的访问级别。

默认值:软件包可见。不需要修改。

私人 : 仅在课堂上可见 。

公众:全世界都能看到。所有班级和包件。

受保护:软件包和所有子类可见。

如果程序员没有指定访问修饰器,则使用默认访问修饰器。

< 强度 > 加入修正器:

  • 公共 - {项目中任何地方都可访问}

    - 私人 - {只能在课内访问}

    - {可在软件包和子类内访问}受保护的 -{可在软件包和子类内访问}

    默认 - {可以访问软件包}

< 强度 > 无法获取的修饰符:

  • 静态 - { 用于创建类变量和方法}

    最终 - {用于创建最后确定的类、变量和方法}

    摘要 - {用于创建抽象方法和类}

    同步 - { for lines}

要学习更多此 < a href=""",http://vhariprasath.blogspot.in/2014/04/modacters-in-java.html" rel="no follow" >link

private and public java access specifiers.in java,private access specifiers it specifies the access.private is like own family property that can uses only which family belongs.that means inside the class,method and variable can be used. public is like government properties that means every one can access the properties without any need of tokens

<强度 > 私人和公共java访问规格 in java ,私人访问规格指定访问。

private is like own family property that can uses only which family belongs..that means inside the class,method and variable can be used

..

public is like government properties that means every one can access the properties without any need of tokens





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签