java中private、static、final、public、abstract关键字的使用模式
我知道除了抽象之外所有这些都做什么。我目前正在自学java,我认为这是中学水平的教育(我的高中在一个不好的社区,所以我被坑了)......
但是这些关键字的使用模式到底是什么?我什么时候用什么?我什么时候忽略它们?将“public”放在我的类前面会使每个使用它的类都需要一个新文件,如果我想创建一个整体源文件,我可以忽略它吗?
我查找的每一条信息都准确地解释了它们的用途,只是没有清楚地说明我应该何时/为何/何处使用它们。
提前致谢, 安东尼
I know what all of these do except for abstract. I'm currently in the process of teaching myself java with what I consider a middle-school-level education (my highschool was in a bad neighborhood so I got shafted)...
But what exactly are the usage patterns for these keywords? When do I use what? When do I omit them? Putting 'public' in front of my classes makes every class that uses it require a new file, can I just omit that if I want to create a monolithic source file?
Every bit of information I look up, explains exactly WHAT these do, just doesn't give a clear view of when/why/where I should use them.
Thanks in advance,
Anthony
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
消息来源告诉我们这些关键字的含义,因为它们的使用时间/原因/地点由此而来。例如,我的解释有“when”一词,但它们直接遵循关键字的语义。
private
protected
应该使用public
当某些东西可以被其他类 三是“可见性修饰符”。当您想要限制某些方法/字段/类对一组对象的使用,并对其他对象隐藏它们时,可以使用它们。还有另一种可见性修饰符 - 默认修饰符(当没有其他修饰符时)。当您希望您的类/方法/字段只能由同一包中的类访问时,可以使用它。static
:abstract
当您不想在当前类中提供实现时:final
- 当您不想更改某些内容时。Sources tell what do these keywords mean because when/why/where they are used follows from that. My explanations have the "when" word, for example, but they follow directly from the semantics of the keywords.
private
should be used when something is not used outside of a given classprotected
should be used whenpublic
is used when something is accessible by every other classThe above three are "visibility modifiers". They are used when you want to limit the usage of some methods/fields/classes to a group of objects, and hide them from other objects. There is another visibility modifier - the default one (when no other is present). It is used when you want your class/method/field to be accessible only to classes from the same package.
static
is used when you don't need an instance of a class (i.e. object) to use it:abstract
when you don't want to provide implementations in the current class:final
- when you don't want something to change.对于初学者,这是我的经验法则:
我强烈建议您抵制使用单一源文件的冲动。尽量让方法少于一屏,让类少于 300 行。
For beginners, here are my rules of thumb:
I would strongly suggest you fight the urge to use one monolithic source file. Try to keep methods shorter than one screenful, and classes shorter than 300 lines.
Bozho 很好地介绍了关键字的用法,但我要补充一点,如果您根本不声明作用域,您的作用域将变为包私有,这意味着与该类位于同一包中的任何人都可以使用该类/方法。基本上,它比
private
更宽松,但比protected
宽松,因为protected
允许从包外部访问。有关“无修饰符”访问的信息位于:
我建议阅读 Java 教程:
如果您想探索更多 Java 知识,还可以看看书上的问题:
Bozho has covered the uses for the keywords pretty well, but I will add that if you do not declare a scope at all, your scope becomes package-private, which means that anyone in the same package as the class can use that class/method. Basically, it's more permissive than
private
, but less permissive than justprotected
, asprotected
allows access from outside a package.Information about the 'no modifier' access here:
I recommend going through the Java tutorial:
And also take a look at the book questions if you want to explore more of Java:
private
、public
和protected
均用于声明 变量类的范围。static
意味着所定义的事物是类的成员,而不是作为类实例的对象。abstract
表示该类不能直接创建,只能被子类使用。抽象方法可以在抽象类中定义,这意味着任何子类都必须定义与定义的签名相匹配的方法。final
意味着变量在创建时只能被分配一次。最终类/方法不能分别被继承/重写。避免将所有内容都放在一个大文件中。使用 IDE(例如 Eclipse),可以轻松处理每个文件一个类的代码。它允许您更好地组织代码并封装代码,这样您就不会陷入一切都知道的情况。这将导致错误,因为更容易意外地使用为实现不同目的而创建的东西。
private
,public
andprotected
are all used for declaring the Scope of a class for variable.static
means that the thing being defined is a member of the class and not an object that is instance of the class.abstract
means that the class can not directly created and can only be used by subclasses. An abstract method can be defined in an abstract class and means that any subclass must define a method matching the defined signature.final
means that the variable can only be assigned a variable once at its creation. A final class/method can not be inherited/overridden, respectively.Stay away from putting everything in one big file. Use an IDE, like Eclipse, and it will make it easy to work with code that has one class per file. It allows you to better organize your code and encapsulate code so you don't end up in a situation where everything knows about everything. This will lead to errors as it becomes easier to accidentally use something the was created to do a different purpose.
私有
方法是一种不能被其引入范围之外的任何其他对象访问的方法。静态方法是属于类而不是类实例的方法。该方法可供类的每个实例访问,但实例中定义的方法只能由类的该成员访问。
final
关键字在多个上下文中使用来定义只能分配一次的实体。public
成员对所有其他类都是可见的。这意味着任何其他类都可以访问public
字段或方法。abstract
方法没有主体,它们只有方法签名。如果一个常规类扩展了一个抽象类,那么该类必须实现抽象父类的所有抽象方法,否则它必须也被声明为抽象A
private
method is a method which can’t be accessed by any other object outside the scope it is introduced.A
static
method is a method that belongs to a class rather than an instance of a class. The method is accessible to every instance of a class, but methods defined in an instance are only able to be accessed by that member of a class.The
final
keyword is used in several contexts to define an entity that can only be assigned once.The
public
members are visible to all other classes. This means that any other class can access apublic
field or method.The
abstract
methods don’t have body, they just have method signature.If a regular class extends an abstract class, then the class must have to implement all the abstract methods of abstract parent class or it has to be declared abstract as well为了理解这些关键字的使用时间/原因/地点,您必须掌握面向对象编程和 Java 的一些关键概念。我建议研究 封装 和 多态。
同样在我的脑海中,我相信“公共”是隐含的,所以它不是必需的,但将它放在那里是一个很好的做法。
In order to understand the when/why/where of these keyword uses, you have to get a grasp on some key concepts of Object Oriented Programming and Java. I suggest looking into Encapsulation and Polymorphism.
Also off the top of my head, I believe 'public' is implied so it isn't required but its good practice to have it there.