简体   繁体   English

java中private,static,final,public,abstract关键字的使用模式

[英]Usage patterns for private, static, final, public, abstract keywords in java

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)... 我目前正在使用我认为的中学教育(我的高中是在一个糟糕的社区,因此我被选中)自学java。

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? 将'public'放在我的类前面使得每个使用它的类都需要一个新文件,如果我想创建一个单片源文件,我可以省略它吗?

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 提前谢谢,安东尼

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. 例如,我的解释有“when”这个词,但它们直接遵循关键词的语义。

  • private should be used when something is not used outside of a given class 当在某个类之外没有使用某些东西时,应该使用private
    • for methods and fields - when they are used only within the same class 对于方法和字段 - 当它们仅在同一个类中使用时
    • for classes - only on nested classes, when used in the same class 对于类 - 仅在嵌套类上使用时,在同一个类中使用
  • protected should be used when 应该使用protected
    • for methods and field - when you need to make them accessible to subclasses only 对于方法和字段 - 当您需要使它们仅对子类可访问时
    • for classes - again only nested classes, accessible by subcalsses 对于类 - 再次只有嵌套类,可以通过子类访问
  • public is used when something is accessible by every other class 当每个其他类都可以访问某些内容时,就会使用public

The 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 (ie object) to use it: 当您不需要类(即对象)的实例来使用它时,将使用static
    • for fields - when you want to have a global field 对于字段 - 当您想拥有全局字段时
    • for methods - when you need utility functions that do not depend on object state for methods - 当您需要不依赖于对象状态的实用程序函数时
    • for nested classes - when you want to access them without an instance of the enclosing class. 对于嵌套类 - 当您想要在没有封闭类的实例的情况下访问它们时。
  • abstract when you don't want to provide implementations in the current class: 当您不想在当前类中提供实现时,请使用abstract
    • on methods - when subclasses have to provide the actual implementation, but you want to invoke these methods (no matter how they are implemented) in this class. on methods - 当子类必须提供实际的实现时,但是你想在这个类中调用这些方法(无论它们是如何实现的)。
    • on classes - to denote that the class may have abstract methods. 在类上 - 表示该类可能有抽象方法。
  • final - when you don't want something to change. final - 当你不想改变什么时。
    • on fields, when you want to assign the value only once. 在字段上,当您只想分配一次值时。 It is useful when you want to pass a local variable to an inner class - you have to declare it final. 想要将局部变量传递给内部类时,它非常有用 - 您必须将其声明为final。
    • on classes and methods - when you don't want subclasses to be able to extend / override them. 关于类和方法 - 当您不希望子类能够扩展/覆盖它们时。

For beginners , here are my rules of thumb: 对于初学者 ,这是我的经验法则:

  1. Public: all classes should be public (this isn't quite true, but it's pretty close). 公共:所有类都应该公开(这不是真的,但它非常接近)。 For methods, think about your TV set: stuff you'd expect to do to your TV is "public". 对于方法,请考虑一下您的电视机:您希望对电视所做的事情是“公开的”。
  2. Private: implementation details should be private. 私有:实施细节应该是私有的。 Think about your TV set: functionality is private if the equivalent kind of thing for a TV should be private, because the user can mess the TV up permanently, get electrocuted, etc. 想想你的电视机:如果电视的同类物品应该是私人的,那么功能是私密的,因为用户可以永久地弄乱电视,触电,等等。
  3. Protected: ignore this for now. 受保护:暂时忽略这一点。
  4. Abstract: The best example I read when learning Java was to think about "Bird". 摘要:我在学习Java时读到的最好的例子是思考“鸟”。 Bird is abstact, and therefore would have an "abstract" flight method. Bird是抽象的,因此将采用“抽象”飞行方法。 Individual species of bird know how to fly (unless they're penguins - then they throw UnsupportedOperationException). 个别鸟类知道如何飞行(除非它们是企鹅 - 然后它们抛出UnsupportedOperationException)。

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. 尝试保持方法短于一个屏幕,并且类别短于300行。

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. Bozho很好地介绍了关键字的用法,但我要补充一点,如果你根本没有声明一个范围,你的范围就变成了包私有,这意味着与该类相同的包中的任何人都可以使用该类/方法。 Basically, it's more permissive than private , but less permissive than just protected , as protected allows access from outside a package. 基本上,它比private更宽松,但更不宽容而不仅仅是protected ,因为protected允许从包外部访问。

Information about the 'no modifier' access here: 有关'无修饰符'访问权限的信息:

I recommend going through the Java tutorial: 我建议通过Java教程:

And also take a look at the book questions if you want to explore more of Java: 如果您想要更多地探索Java,还要看一下本书的问题:

private , public and protected are all used for declaring the Scope of a class for variable. privatepublicprotected都用于声明变量类的范围

static means that the thing being defined is a member of the class and not an object that is instance of the class. static意味着被定义的东西是类的成员而不是作为类的实例的对象。

abstract means that the class can not directly created and can only be used by subclasses. abstract表示该类不能直接创建,只能由子类使用。 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. final表示变量只能在创建时赋值一次。 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. 使用像Eclipse这样的IDE,它可以很容易地处理每个文件有一个类的代码。 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. 这将导致错误,因为它更容易意外地使用创建的东西来实现不同的目的。

A private method is a method which can't be accessed by any other object outside the scope it is introduced. private方法是一种方法,它不能被引入范围之外的任何其他对象访问。

A static method is a method that belongs to a class rather than an instance of a class. static方法是属于类而不是类的实例的方法。 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. 在几个上下文中使用final关键字来定义只能分配一次的实体。

The public members are visible to all other classes. 所有其他类都可以看到public成员。 This means that any other class can access a public field or method. 这意味着任何其他类都可以访问public字段或方法。

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 abstract方法没有body,它们只有方法签名。如果常规类扩展了一个抽象类,那么该类必须实现抽象父类的所有抽象方法,或者它必须被声明为abstract

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. 为了理解这些关键字使用的时间/原因/位置,您必须掌握面向对象编程和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. 同样在我的头脑中,我相信'公共'是隐含的,所以它不是必需的,但它的良好做法就是把它放在那里。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM