私有方法优于公共方法

发布于 2024-12-09 07:43:13 字数 781 浏览 0 评论 0原文

我正在检查 StringTokenizer.java 类,并想到了一些问题。

我注意到其他类使用的公共方法调用了一些完成所有工作的私有方法。现在,我知道 OOD 的原则之一是尽可能私有化并隐藏所有实现细节。但我不确定我是否完全理解这背后的逻辑。

我知道将字段设为私有以防止在其中存储无效值非常重要(这只是众多原因之一)。然而,当谈到私有方法时,我不确定为什么它们如此重要。

例如,对于 StringTokenizer 类,我们不能将所有实现代码放在公共方法中吗?由于这些方法的 API(即调用这些公共方法的规则)将保持不变,这会对使用这些方法的类产生什么影响?我能想到私有方法有用的唯一原因是它可以帮助您编写重复的代码。例如,如果所有公共方法都执行相同的操作,那么您可以声明一个私有方法来执行此任务并且可由公共方法使用。

另一个问题,与公共方法相比,用私有方法编写实现有什么好处?

这是一个小例子:

public class Sum{

    private int sum(int a, int b){
        return a+b;
    }

    public int getSum(int a, int b){
        return sum(a,b);
    }
}

VS...

public class Sum{

    public int getSum(int a, int b){
        return a+b;
    }
}

第一个样本如何更有利?

I was examining the StringTokenizer.java class and there were a few questions that came to mind.

I noticed that the public methods which are to be used by other classes invoked some private method which did all of the work. Now, I know that one of the principles of OOD is to make as much as you can private and hide all of the implementation details. I'm not sure I completely understand the logic behind this though.

I understand that it's important to make fields private to prevent invalid values being stored in them (just one of many reasons). However, when it comes to private methods, I'm not sure why they're as important.

For example, in the case of the StringTokenizer class, couldn't we just have put all of the implementation code inside the public methods? How would it have made a difference to the classes which use these methods since the API for these methods (i.e. the rules to call these public methods) would remain the same? The only reason I could think of why private methods are useful is because it helps you from writing duplicate code. For example, if all of the public methods did the same thing, then you can declare a private method which does this task and which can be used by the public methods.

Other question, what is the benefit of writing the implementation in a private method as opposed to a public method?

Here is a small example:

public class Sum{

    private int sum(int a, int b){
        return a+b;
    }

    public int getSum(int a, int b){
        return sum(a,b);
    }
}

Vs...

public class Sum{

    public int getSum(int a, int b){
        return a+b;
    }
}

How is the first sample more beneficial?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

乱了心跳 2024-12-16 07:43:13

为了添加一些东西,私有方法总是可以安全地更改,因为您确信它只能从自己的类中调用,没有外部类能够调用私有方法(它们甚至看不到它)。

因此,拥有一个私有方法总是好的,因为您知道更改它没有问题,即使您可以安全地向该方法添加更多参数。

现在考虑一个公共方法,任何人都可以调用该方法,因此如果添加/删除参数,则还需要更改对该方法的所有调用。

In order to add something, a private method can ALWAYS be changed safely, because you know for sure that is called only from the own class, no external classes are able to call a private method (they can't even see it).

So having a private method is always good as you know there is no problem about changing it, even you can safely add more parameters to the method.

Now think of a public method, anyone could call that method, so if you add/remove a parameter, you will need to change also ALL the calls to that method.

不念旧人 2024-12-16 07:43:13

我能想到私有方法有用的唯一原因是它可以帮助您编写重复的代码。

除了合并重复代码(通常表示为“不要重复自己”或“DRY”)之外,使用私有方法还可以帮助您构建和记录代码。如果您发现自己编写的方法可以执行多项操作,那么您可能希望考虑将其拆分为多个私有方法。这样做可以更清楚地了解每个逻辑的输入和输出是什么(更精细的粒度)。此外,描述性方法名称可以帮助补充代码文档。

The only reason I could think of why private methods are useful is because it helps you from writing duplicate code.

In addition to consolidating duplicate code (often expressed as "Don't Repeat Yourself" or "DRY"), use of private methods can also help you to structure and document your code. If you find yourself writing method which does several things, you may wish to consider splitting it into several private methods. Doing so may make it clearer what the inputs and outputs for each piece of logic are (at a finer granularity). Additionally, descriptive method names can help supplement code documentation.

跨年 2024-12-16 07:43:13

当用 Java 或任何其他面向对象的语言编写干净的代码时,通常最干净、最可读的代码由简短的方法组成。人们经常会发现,方法内的逻辑可以更好地用单独的方法调用来表达,以使代码更清晰、更易于维护。

考虑到这一点,我们可以设想这样的情况:您可以使用多种方法来执行单个目标的任务。想象一个只有一个复杂目的的类。该单一目标的入口点可能只需要一个起点(一个公共方法),但需要许多其他方法,这些方法是复杂操作的一部分(许多私有帮助方法)。

使用私有方法,我们能够隐藏不能也不应该从类本身之外的任何地方访问的逻辑。

When writing clean code in Java or any other object-oriented language, in general the cleanest most readable code consists of short concise methods. It often comes up that the logic within a method could be better expressed in separate method calls to make the code cleaner and more maintainable.

With this in mind, we can envision situations where you have many methods performing tasks towards a single goal. Think of a class which has only one single complex purpose. The entry point for that single goal may only require one starting point (one public method) but many other methods which are part of the complex operation (many private helping methods).

With private methods we are able to hide the logic which is not and should not be accessible from anywhere outside of the class itself.

别忘他 2024-12-16 07:43:13

公共方法通常是实现该类的其他类想要使用的代码。私有方法通常在类之外没有那么有用,或者不能(单独)达到类要完成的目的。

假设您在选择的 IDE 中,实现了某个 A 类。A 类仅设计用于完成一件事,即文档生成。当然,A 类中会有一些生成文档所需的数学和字节操作方法,但尝试使用 A 类的人不会需要这些其他方法,因为他们只想要一个文档。因此,我们将这些方法设为私有,以便让我们类的未来用户变得简单。

Public methods are generally code that other classes which implement that class will want to use. Private methods are generally not as useful outside the class, or don't(alone) serve the purpose of what the class is meant to accomplish.

Say you're in your IDE of choice, and you implement a some class A. Class A is only designed to do one thing, say document generation. Naturally you will have some mathematical and byte operation methods in Class A that are required to do document generation, but people trying to use Class A are not going to need these other methods, because they just want a document. So we make these methods private to keep things simple for any future users of our class.

一生独一 2024-12-16 07:43:13

将方法声明为私有的目的是

  • 隐藏实现细节,
  • 排除该方法被列为公共 API,
  • 确保代码背后的逻辑
  • 在大多数情况下不会在外部使用/误用,您的方法的执行取决于在其之前运行的其他方法;那么您还可以确保控制使用方法的正确顺序

对您的方法使用 private 除非您打算在类上下文之外安全地使用您的方法。

The purpose of declaring a method private is to

  • hide implementation details
  • exclude the method from being listed as public API
  • make sure the logic behind the code is not used/misused externally
  • most of the time your method's execution depends on other methods being run before it; then you can also be sure that you control the correct sequence of using your method

Use private for your methods unless you intend for your method to be safely used outside of the context of your class.

隱形的亼 2024-12-16 07:43:13

将函数设置为私有可以在以下情况下给您带来优势:

  1. 将函数设置为私有可以让 JVM 编译器选择内联该函数,从而提高应用程序性能
  2. 如果该类是可继承的并且您从子类扩展它,那么如果您想要隐藏子类中的函数,然后您可以执行此操作(您可以扩展 StringTokenizer)。
  3. 如果一段代码必须在多个函数中使用,则可以将该代码移动到私有实用程序方法中

Making functions private gives you advantage in following cases :

  1. Making function private gives JVM compiler the option of inlining the function and hence boosting up the application performance
  2. If the class is inheritable and you extend it from a child class, then in case if you want to hide the functions from child class then you can do this (you can extend StringTokenizer).
  3. If a piece of code has to be used in multiple functions the you move that code in private utility method
红颜悴 2024-12-16 07:43:13

在公共类中使用私有方法的一个优点也是一个很好的理由是为了安全和预防错误。声明为私有的方法只能由它们所属的类访问。这意味着您的私有方法不会被意外地从程序中的其他位置调用,从而减少错误和其他复杂性。如果您将方法声明为公共方法,则整个问题都可以访问它,并且可能会导致复杂化。

您可能有许多方法可以处理特定的数据,您不希望程序的任何其他部分能够干扰这些数据。通过私有方法和/或变量使用数据封装有助于防止这种情况发生,并且还使您的代码更易于遵循和记录。

An advantage and also a good reason to use private methods inside public classes is for security and bug prevention. Methods that are declared as private are only accessible by the class they are part of. This means that your private method can't be accidentally called from else where within the program reducing bugs and other complications. If you declare your method as public it can be accessed by the whole problem and can cause complications.

You may have a number of methods that work on a certain piece of data that you don't want any other part of the program to be able to interfere with. Using data encapsulation via private methods and/or variables helps to prevent this and also makes your code easier to follow and document.

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