简体   繁体   English

在Java中,如果您的方法具有未指定的可见性关键字,会发生什么?

[英]In Java, what happens when you have a method with an unspecified visibility keyword?

I have been working with android for a few years now, not once have I had a teacher or anyone to tell me what to do. 我已经和android一起工作了几年,没有一次我有老师或任何人告诉我该怎么做。 This whole time I have wondered to myself this. 这整个时间我都在想这个。

When you have a method I generally see... 当你有一个方法我通常会看到......

public void method(){
//Stuff
}

or 要么

private void method(){
//stuff
}

I know that a void is a method with no return value, and that public is the visibility of the method in a way but would it matter if I just used something like this... 我知道void是一种没有返回值的方法,而public是方法的可见性,但是如果我只是使用这样的东西就行了......

void method(){
//stuff
}

Because then the methods visibility would just be default anyway? 因为方法可见性只是默认的默认值?

I have no idea if I am right or not, is it just good practice to specify "public" or "private" ? 我不知道我是否对,是指定“公共”还是“私人”的好习惯?

Not specifying anything has a specific meaning: 未指定任何内容具有特定含义:

  • public - any class can access this member public - 任何类都可以访问此成员
  • protected - subclasses can access this member (as well as code in the same class or in the same package) protected - 子类可以访问此成员(以及同一个类或同一个包中的代码)
  • private - only code in the same class can access this member private - 同一类中的唯一代码可以访问此成员
  • nothing ("default" access) - only code in the same package can access this member 没有(“默认”访问) - 只有同一个包中的代码才能访问该成员

Arguably the last case should have had its own keyword, but we're stuck with it now. 可以说最后一个案例应该有自己的关键字,但我们现在坚持使用它。 Unless you really mean to use default visibility, it's poor form to not specify anything - did you really need package visibility for some reason, or did you just default to package visibility for everything? 除非你真的想要使用默认可见性,否则不能指定任何内容的形式很差 - 你是否真的因某种原因需要包可见性,或者你只是默认包装可见性? Best practice is to explicitly use private for non-public members unless you need one of the others. 最佳做法是明确使用private非公共成员,除非您需要其中一个成员。

Java has four levels of visibility: public, protected, (default), private. Java有四个级别的可见性:public,protected,(default),private。 The meaning of these is as follows: 这些含义如下:

  1. public - makes your methods accessible to any other class. public - 使您的方法可供任何其他类访问。
  2. protected - makes your methods accessible to any class in the same package OR any subclass of your class. protected - 使您的方法可以被同一个包中的任何类或类的任何子类访问。
  3. (default, ie no modifier) - makes your methods accessible only to classes in the same package. (默认,即无修饰符) - 使您的方法只能访问同一个包中的类。
  4. private - makes your methods accessible only to the current class. private - 使您的方法只能访问当前类。

The same rules apply when specifying the access modifiers on classes, methods and fields. 在类,方法和字段上指定访问修饰符时,适用相同的规则。

Java has four levels of visibility: public, protected, (default), private Java有四个级别的可见性:public,protected,(default),private

  1. Visible to the package. 包装可见。 the default. 默认。 No modifiers are needed. 不需要修饰符。
  2. Visible to the class only (private). 仅对班级可见(私人)。
  3. Visible to the world (public). 对世界可见(公众)。
  4. Visible to the package and all subclasses (protected). 包和所有子类(受保护)可见。

在此输入图像描述

Default Access Modifier - No keyword: 默认访问修饰符 - 无关键字:

Default access modifier means we do not explicitly declare an access modifier for a class, field, method etc. 默认访问修饰符意味着我们没有为类,字段,方法等显式声明访问修饰符。

A variable or method declared without any access control modifier is available to any other class in the same package. 声明没有任何访问控制修饰符的变量或方法可用于同一包中的任何其他类。 The default modifier cannot be used for methods, fields in an interface. 默认修饰符不能用于接口中的方法,字段。

Private Access Modifier - private: 私人访问修改器 - 私人:

Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself. 声明为private的方法,变量和构造函数只能在声明的类本身中访问。

Private access modifier is the most restrictive access level. 专用访问修饰符是限制性最强的访问级别。 Class and interfaces cannot be private. 类和接口不能是私有的。

Variables that are declared private can be accessed outside the class if public getter methods are present in the class. 如果类中存在公共getter方法,则可以在类外部访问声明为private的变量。

Using the private modifier is the main way that an object encapsulates itself and hide data from the outside world. 使用私有修饰符是对象封装自身并从外部世界隐藏数据的主要方式。

Public Access Modifier - public: 公共访问修饰符 - 公共:

A class, method, constructor, interface etc declared public can be accessed from any other class. 可以从任何其他类访问声明为public的类,方法,构造函数,接口等。 Therefore fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe. 因此,可以从属于Java Universe的任何类访问在公共类中声明的字段,方法,块。

However if the public class we are trying to access is in a different package, then the public class still need to be imported. 但是,如果我们尝试访问的公共类位于不同的包中,则仍需要导入公共类。

Because of class inheritance, all public methods and variables of a class are inherited by its subclasses. 由于类继承,类的所有公共方法和变量都由其子类继承。

Protected Access Modifier - protected: 受保护的访问修饰符 - 受保护:

Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class. 在超类中声明受保护的变量,方法和构造函数只能由其他包中的子类或受保护成员类的包中的任何类访问。

The protected access modifier cannot be applied to class and interfaces. 受保护的访问修饰符不能应用于类和接口。 Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected. 方法,字段可以声明为protected,但是接口中的方法和字段不能声明为protected。

Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it. 受保护的访问权限使子类有机会使用辅助方法或变量,同时防止非相关类尝试使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 框架:当您将 Java bean 注入另一个 Java bean 时会发生什么,但它们具有不同的范围 - Spring framework: what happens when you inject a Java bean into another Java bean but they have different scopes 在方法中有最终值和内部类时会发生什么? - What exactly happens when you have final values and inner classes in a method? 在 Java 中重写方法时会发生什么? - What happens when a method is overridden in Java? 从java中的非同步方法调用synchronized时会发生什么 - what happens when you call a synchronized from a non-synchronized method in java 将.method放在对象之后会发生什么? - What happens when you put a .method after an object? 在已经工作的线程中调用方法时会发生什么? - What happens when you call a method in a thread that is already working? 当您在方法内部更改引用时会发生什么? (7) - What happens when you change a reference inside a method? (7) 当您关闭电源时,Java 程序会发生什么? - What happens to a Java program when you turn the power off? 当你在java中将一个数组分配给另一个数组时会发生什么? - What happens when you assign an array to another array in java? 当您使用super和此关键字调用替代方法时会发生什么? - What happen when you call an override method using super and this keyword?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM