简体   繁体   English

为什么 JAVA 源代码中的方法调用另一个方法,即使调用方法只是做一个调用工作?

[英]Why method in JAVA source code calls another method even though calling method just do a call job?

When I was reading source code of LinkedList, I find a strange phenomenon(Maybe starnge just to me....) that many public method do nothing else but only call another private method like below.当我阅读 LinkedList 的源代码时,我发现一个奇怪的现象(也许对我来说很奇怪......)许多公共方法什么都不做,只是调用另一个私有方法,如下所示。

public void addFirst(E e) {
    linkFirst(e);
}
public void addLast(E e) {
    linkLast(e);
}

Thus, could anybody tell me why method should be written like this, thanks a lot因此,谁能告诉我为什么方法应该这样写,非常感谢

As Jeroen comments, this is often done to avoid writing multiple methods that do the same thing when a class is implementing (directly or indirectly) multiple interfaces, and the interfaces methods have overlapping semantics.正如Jeroen评论的那样,当 class 正在实现(直接或间接)多个接口并且接口方法具有重叠的语义时,通常这样做是为了避免编写执行相同操作的多个方法。

But in this case, linkFirst and linkLast are internal methods.但在这种情况下, linkFirstlinkLast是内部方法。 Here, the reason appears to be that the code has been reorganized, and this is consequence of that reorganization.在这里,原因似乎是代码已被重组,这是重组的结果。 The Java 6 source code implements these methods as follows: Java 6 源码实现这些方法如下:

public void addFirst(E e) {
    addBefore(e, header.next);
}

public void addLast(E e) {
    addBefore(e, header);
}

Does it matter?有关系吗? Is it inefficient?效率低吗?

Probably not.可能不是。 The JIT compiler is likely inline the addFirst and addLast methods wherever they are called. JIT 编译器很可能内联addFirstaddLast方法,无论它们被调用。 But even if it doesn't the overhead of the unnecessary call will be tiny.但即使它不是不必要的调用的开销也会很小。

Well, the Methods addFirst(E e) & addLast(E e) are both required, as java.util.LinkedList implements the interface java.util.Deque where these 2 methods are specified.好吧,方法addFirst(E e)addLast(E e)都是必需的,因为java.util.LinkedList实现了接口java.util.Deque ,其中指定了这两种方法。

Nowadays, you would use the @Override annotation on those 2 implementations to indicate you know that to the compiler, but back then when much of this code was written annotations weren't that widespread (or even invented?).如今,您会在这两个实现上使用@Override注释来向编译器表明您知道这一点,但是在当时编写大部分代码时,注释并没有那么普遍(甚至是发明的?)。 (Annotations first became publicly available in JDK 1.5) (注释首先在 JDK 1.5 中公开可用)

Now, linkFirst(E e) is private & only called from addFirst(E e) , so maybe you could argue it could be inlined, but maybe someone decided it would be prudent to protect that logic as addFirst(E e) is public & may be subclassed.现在, linkFirst(E e)是私有的并且只能从addFirst(E e)调用,所以也许你可以争辩它可以被内联,但也许有人认为保护该逻辑是谨慎的,因为addFirst(E e)是公共的 &可以细分。

linkLast(E e) has default visibility & is called from many places, so it may be invoked from other members in the java.util package, but again it may have been deemed prudent to not expose this logic as addLast(E e) may be subclassed. linkLast(E e)具有默认可见性并从许多地方调用,因此可以从java.util package 中的其他成员调用它,但同样,谨慎的做法可能是不要公开此逻辑,因为addLast(E e)可能被子类化。

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

相关问题 为什么即使该调用位于if语句内部,该递归方法仍会继续调用自身? - Why does this recursive method keeps calling itself even though the call is inside if statement? Java - 即使调用方法,JPanel也不会绘制 - Java - JPanel do not paint even though method is called 一个java类方法可以调用另一个java类方法。 那你为什么需要RequestDispatcher? - A java class method can call another java class method. Then why do you need RequestDispatcher? load方法在Java的最后调用另一个方法 - load method calls another method at the end in Java 基于方法调用的eclipse格式java源代码 - eclipse format java source code based on method calls 覆盖的Java方法即使存在也不会被调用 - Overridden Java method not called even though it exists Java方法即使不使用也需要参数 - Java method requires argument even though it is not used Java:NoSuchMethodException 即使该方法存在 - Java: NoSuchMethodException even though the method exists Java:如何调用在接口中实现另一个方法的方法? - Java: How do I call a method that implements another method in an interface? 如果仅在Java中调用另一个线程安全方法,该方法是否是线程安全的? - Is a method thread-safe if it just calls another thread safe method in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM