简体   繁体   English

为什么我们不应该从另一个公共调用公共方法?

[英]Why we should not call public method from another public?

I was told by someone that we should not call a public method of a class from another public method in same class.有人告诉我,我们不应该从同一个类中的另一个公共方法调用一个类的公共方法。 Now i am not able to understand this as i dont see any problem with that.现在我无法理解这一点,因为我没有看到任何问题。 Once a method has been declared public then its contract is fixed for lifetime and hence there should not be any problem in calling it from another public method.一旦一个方法被声明为 public ,那么它的契约在生命周期内是固定的,因此从另一个公共方法调用它应该没有任何问题。

So I am not sure if that statement is true or its ok to call public api from another public api [This is from design perspective]?因此,我不确定该陈述是否属实,还是可以从另一个公共 api 调用公共 api [这是从设计角度来看的]?

Does your compiler balk at you when you try?当您尝试时,您的编译器是否对您犹豫不决? No?不? Then it's legal in that respect.那么在这方面它是合法的。

Does the person providing this 'advice' produce any canonical document explaining the standard (either in the industry or within your organisation)?提供此“建议”的人是否制作了任何解释标准的规范文件(在行业内或在您的组织内)? No?不? Then it's opinion.然后是意见。

Consult your company standards, but otherwise, I call nonsense.咨询贵公司的标准,否则,我称之为废话。

If you call a public methods from other public methods it makes unit testing more complicated.如果您从其他公共方法调用公共方法,则会使单元测试变得更加复杂。 If one method is depended on another method of the same class you can't mock it to test it separately.如果一个方法依赖于同一个类的另一个方法,你不能模拟它来单独测试它。 So you may have to write test code for the same method twice.因此,您可能必须为同一方法编写两次测试代码。

see also Unit testing a method that calls another method另请参见对调用另一个方法的方法进行单元测试

When you very often call public methods from other public methods of the same class, it probably means you have unnecessary utilities methods.当您经常从同一类的其他公共方法调用公共方法时,这可能意味着您有不必要的实用程序方法。 And maybe you should try to be a little more DRY so as to ease maintenance and keep your API easy to grasp.也许您应该尝试更 DRY 以简化维护并保持您的 API 易于掌握。

But that's just a warning, it's perfectly valid to call a public method from another one, and you'll find many examples of java.lang standard code doing just that.但这只是一个警告,从另一个方法调用公共方法是完全有效的,您会发现很多java.lang标准代码的例子就是这样做的。

An example from java.lang.String :来自java.lang.String一个例子:

1462    public boolean startsWith(String prefix) {
1463        return startsWith(prefix, 0);
1464    }

There would be no point in making some methods private just to enforce a rule about public methods not calling other public methods.仅仅为了强制执行关于公共方法而不调用其他公共方法的规则而将某些方法设为私有是没有意义的。

In my opinion you should avoid call a public method within another method because of inheritance.在我看来,由于继承,您应该避免在另一个方法中调用公共方法。 Consider classes:考虑类:

public class Parent {

    //return sum
    public double getSum(double... value){
        //implementation
    }

    //return average
    public double getAverage(int count){
        //call getSum
        double sum = getSum(20, 40, 60);
        return sum / count;

    }
}

public class Child extends Parent {

    @Override
    public int getSum(double... obj){
        // change implementation
        // always return 100;
    }
}

If you called on Child object getAverage method, you would get some unexpected value, whole interface of Child object is broken.如果你调用 Child 对象的 getAverage 方法,你会得到一些意想不到的值,Child 对象的整个接口都被破坏了。 You have to override getAverage method too...您也必须覆盖 getAverage 方法...

Example with String class字符串类示例

1462    public boolean startsWith(String prefix) {
1463        return startsWith(prefix, 0);
1464    }

is from this perspective wrong, because string is final, so you can't inherit it and override its public methods..从这个角度来看是错误的,因为字符串是最终的,所以你不能继承它并覆盖它的公共方法..

I just summarize answers here and add some context:我只是在这里总结答案并添加一些上下文:

  • It is legal compile-wise to reuse public methods from public methods within the same class.在同一类中重用公共方法中的公共方法是合法的编译明智的。
  • It is considered a bad practice for API design,because it makes your class designed error-prone for inheritance given one may override one method without overriding another, breaking the logic of your class (this point is from one famous Java "best-practices" book "Effective Java" and it is well explained there and it is also well explained in another answer in this question)这被认为是 API 设计的一种不好的做法,因为它使您的类设计容易为继承而出错,因为一个人可能会覆盖一个方法而不覆盖另一个方法,从而破坏了您的类的逻辑(这一点来自一个著名的 Java“最佳实践”)书“Effective Java”,它在那里得到了很好的解释,在这个问题的另一个答案中也得到了很好的解释)
  • Also it is mentioned in another answer, you can not mock public method, so it may be concern for unit-testing在另一个答案中也提到了,您不能模拟公共方法,因此可能需要关注单元测试

My 2 cents:我的 2 美分:

  • Making a class "final" makes it not a bad practice anymore, as you do not design your API for inheritance.使类“最终”使其不再是一个坏习惯,因为您没有设计用于继承的 API。
  • I don't see a concern with unit-testing, you can write your test the same way as you would write it if one of your methods is private.我没有看到单元测试的问题,如果您的方法之一是私有的,您可以像编写测试一样编写测试。

I am not sure what he meant.我不确定他的意思。 You can obviously call a public method from another public method.您显然可以从另一个公共方法调用一个公共方法。 It is not a bad idea - even Java's source has methods in which one public method calls another public method.这不是一个坏主意 - 即使 Java 的源代码也有方法,其中一个公共方法调用另一个公共方法。

Why don't you ask the guy who advised you to explain the reason?你为什么不问问建议你的人解释原因? I think he is in a better position to tell than we are.我认为他比我们更有发言权。

Of course you can call any public method from another public method whether it is inside same class or another class.当然,您可以从另一个公共方法调用任何公共方法,无论它是在同一个类中还是在另一个类中。 But standard does matter for different organization.但是标准对于不同的组织来说确实很重要。 Give him/her some valid example.给他/她一些有效的例子。

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

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