[英]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
- 同一类中的唯一代码可以访问此成员 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:
这些含义如下:
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
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.