内部也调用外部(即接口)函数吗?

发布于 2024-07-17 04:17:57 字数 299 浏览 8 评论 0原文

我想知道在内部调用公共函数是否是一个好习惯。

我所说的公共函数是指您显式创建的从其他对象、模块等调用的所有方法/函数。例如,您通常会放入 Java 接口定义中的方法。 通过内部调用,我的意思是在同一个模块、类等中。

我总是感觉这在某种程度上“滥用”公共方法,尽管我想不出任何有效的技术理由不这样做做吧。

另一种方法是将函数体放在私有函数/方法中,您可以在内部调用它。 然后,公共方法的主体将包含对私有函数的单个调用。

我认为这个问题是非常主观的,但仍然......对此有什么想法吗?

I am wondering if it is good practice to call public functions also internally.

By public functions I mean all methods/functions that you created explicitly to be called from other objects, modules, etc. For example methods that you would typically put in a Java interface definition. By calling internally, I mean in the same module, class, etc.

I've always felt about this as somehow 'misusing' public methods, although I can't think of any valid technical reason not to do it.

The alternative would be to put the body of the function in a private function/method, which you can invoke internally. The body of the public method would then consist of a single call to the private function.

I image this question is highly subjective, but still... Any thoughts on this?

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

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

发布评论

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

评论(2

∞琼窗梦回ˉ 2024-07-24 04:17:57

没有理由不在内部使用公共方法。 不要将私有方法包装在公共方法中,这样做没有任何好处,而且会使您的代码稍微不太清晰。

There is no reason not to use public methods internally. Don't wrap private methods in public methods, there's no benefit to it and it makes you code slightly less clear.

听你说爱我 2024-07-24 04:17:57

是的,你可以这样做,这是一种很好的做法,但必须根据情况认真考虑。

两个例子:

  • 小心不要在类构造函数中调用方法:在调用期间,您的类已部分初始化,因此对方法(类本身或继承的)的任何调用都可能会产生不良且难以跟踪的后果。 根据经验,构造函数必须对类“自我满足”。

  • 由于您正在使用该类的方法,因此您可以自由访问其实现。 问问自己“我想通过其接口还是通过其实现来访问该类?”。

为了更好地解释第二点,假设您有一个带有方法 foo() 和 bar() 的类 A。

假设 foo() 在其代码中调用 bar()。 在这种情况下,您正在与对象的接口进行对话。 这个界面不可能是你所期望的! 例如,假设该类已被您以及代码的用户重新实现(类 B,从 A 派生),并且该方法 bar() 已被重写。 由于继承关系,A::foo() 将调用 B::bar(),而不是 A::bar()。

通过不使用 bar() 方法调用,而是访问实现,可以保证您的函数行为相同,即使对于子类也是如此。

其中一些问题涉及所谓的脆弱基类问题。 这是关于它的一篇非常好的文章

Yes, you can do it, it is a fine practice which must however be considered strongly with respect to the situation.

Two examples:

  • Be careful not to call methods in the class constructor: during the call your class is partially initialized, so any call to methods (of the class itself or inherited) could have bad and hard to track consequences. As a rule of thumb, the constructor must be "self-satisfied" with respect to the class.

  • Since you are using methods of the class, you have free access to its implementation. Ask yourself "Do I want to access the class through its interface, or through its implementation?".

To better explain the second point, suppose you have a class A with methods foo() and bar().

Suppose that foo() calls bar() in its code. In this case you are talking with the interface of your object. This interface could not be the one you expect! For example, suppose that the class has been reimplemented (class B, derived from A) by you, but also by an user of your code, and that method bar() has been overridden. Due to the inheritance, A::foo() will call B::bar(), instead of A::bar().

By not using the bar() method call, and accessing the implementation instead, you are guaranteed that your function behaves the same, even for child classes.

Some of these issues enter in the so-called fragile base class problem. Here is a very good article about it.

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