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

发布于 2024-10-18 13:01:32 字数 252 浏览 7 评论 0原文

我知道除了抽象之外所有这些都做什么。我目前正在自学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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

节枝 2024-10-25 13:01:32

消息来源告诉我们这些关键字的含义,因为它们的使用时间/原因/地点由此而来。例如,我的解释有“when”一词,但它们直接遵循关键字的语义。

  • 当在给定类之外未使用某些内容时,应使用 private
    • 对于方法和字段 - 当它们仅在同一个类中使用时
    • 对于类 - 仅在嵌套类上,当在同一个类中使用时
  • protected 应该使用
    • 对于方法和字段 - 当您需要使它们仅可供子类访问时
    • 对于类 - 同样只有嵌套类,可以通过子类访问
  • 访问时使用

public 当某些东西可以被其他类 三是“可见性修饰符”。当您想要限制某些方法/字段/类对一组对象的使用,并对其他对象隐藏它们时,可以使用它们。还有另一种可见性修饰符 - 默认修饰符(当没有其他修饰符时)。当您希望您的类/方法/字段只能由同一包中的类访问时,可以使用它。

  • 当你不需要类(即对象)的实例来使用它时,使用static
    • 对于字段 - 当您想要一个全局字段时
    • 对于方法 - 当您需要不依赖于对象状态的实用函数时
    • 对于嵌套类 - 当您想要在没有封闭类实例的情况下访问它们时。
  • abstract 当您不想在当前类中提供实现时:
    • 方法上 - 当子类必须提供实际实现,但您想在此类中调用这些方法(无论它们如何实现)时。
    • 在类上 - 表示该类可以具有抽象方法。
  • final - 当您不想更改某些内容时。
    • 在字段上,当您只想分配一次值时。当您想要将局部变量传递给内部类时,它很有用 - 您必须将其声明为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 class
    • 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
    • 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

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 (i.e. object) to use it:
    • 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 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:
    • 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 classes - to denote that the class may have abstract methods.
  • final - when you don't want something to change.
    • 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.
    • on classes and methods - when you don't want subclasses to be able to extend / override them.
〃安静 2024-10-25 13:01:32

对于初学者,这是我的经验法则:

  1. 公共:所有类都应该是公共的(这不完全正确,但非常接近)。对于方法,请考虑一下您的电视机:您希望对电视执行的操作是“公开的”。
  2. 私有:实现细节应该是私有的。想想你的电视机:如果电视的同等功能应该是私有的,那么功能就是私有的,因为用户可能会永久弄乱电视、触电等。
  3. 受保护:暂时忽略它。
  4. 摘要:我在学习 Java 时读到的最好的例子就是思考“Bird”。鸟是抽象的,因此会有“抽象”的飞行方法。个别鸟类知道如何飞行(除非它们是企鹅 - 然后它们会抛出 UnsupportedOperationException)。

我强烈建议您抵制使用单一源文件的冲动。尽量让方法少于一屏,让类少于 300 行。

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". Bird is abstact, and therefore would have an "abstract" flight method. Individual species of bird know how to fly (unless they're penguins - then they throw 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.

过潦 2024-10-25 13:01:32

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 just protected, as protected 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:

无法回应 2024-10-25 13:01:32

privatepublicprotected 均用于声明 变量类的范围

static 意味着所定义的事物是类的成员,而不是作为类实例的对象。

abstract表示该类不能直接创建,只能被子类使用。抽象方法可以在抽象类中定义,这意味着任何子类都必须定义与定义的签名相匹配的方法。

final 意味着变量在创建时只能被分配一次。最终类/方法不能分别被继承/重写。

避免将所有内容都放在一个大文件中。使用 IDE(例如 Eclipse),可以轻松处理每个文件一个类的代码。它允许您更好地组织代码并封装代码,这样您就不会陷入一切都知道的情况。这将导致错误,因为更容易意外地使用为实现不同目的而创建的东西。

private, public and protected 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.

最好是你 2024-10-25 13:01:32

私有方法是一种不能被其引入范围之外的任何其他对象访问的方法。

静态方法是属于类而不是类实例的方法。该方法可供类的每个实例访问,但实例中定义的方法只能由类的该成员访问。

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 a public 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

執念 2024-10-25 13:01:32

为了理解这些关键字的使用时间/原因/地点,您必须掌握面向对象编程和 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文