`
RednaxelaFX
  • 浏览: 3052444 次
  • 性别: Icon_minigender_1
  • 来自: 海外
社区版块
存档分类
最新评论

Java 8的default method与method resolution

    博客分类:
  • Java
阅读更多
先看看下面这个代码例子,
interface IFoo {
  default void bar(int i) {
    System.out.println("IFoo.bar(int)");
  }
}

public class Foo implements IFoo {
  public static void main(String[] args) {
    Foo foo = new Foo();
    foo.bar(42);  // (1) invokevirtual Foo.bar(int)void

    IFoo ifoo = foo;
    ifoo.bar(42); // (2) invokeinterface IFoo.bar(int)void
  }

  public void bar(long l) {
    System.out.println("Foo.bar(long)");
  }
}

(1)与(2)分别应该调用哪个版本的方法呢?

Java 8的接口上的default method,或者叫virtual extension method,目的是为了让接口可以“事后”添加新方法而无需强迫所有实现该接口的类都提供新方法的实现。也就是说它的主要使用场景可能会涉及“代码演进”。

所以让我们把例子退回到代码演进的更早阶段。或许以前这段代码是这样的:
interface IFoo {
}

public class Foo implements IFoo {
  public static void main(String[] args) {
    Foo foo = new Foo();
    foo.bar(42);  // (1) invokevirtual Foo.bar(long)void
  }

  public void bar(long l) {
    System.out.println("Foo.foo(long)");
  }
}

此时的(1)处会调用到Foo.bar(long)方法。

但是当IFoo新添加了新方法bar(int)并提供默认实现之后,(1)就会被“劫持”到IFoo.bar(int)的默认实现上,因为这个版本的signature提供了更准确的匹配。

这种“劫持”行为似乎很符合Java的一贯语义,但是很容易给码农挖坑啊orz…

当前版本的Java 8语言规范草案:http://cr.openjdk.java.net/~mr/se/8/java-se-8-fr-spec-01/java-se-8-jls-fr-diffs.pdf
分享到:
评论
1 楼 funnyone 2018-11-03  
    

相关推荐

    java面试宝典

    8. **".java"源文件中是否可以包括多个类**: - 可以包含多个类,但只能有一个公共类(public class),且文件名必须与公共类名一致。 - 其他类可以是默认访问级别,即不使用任何访问修饰符声明。 9. **char型...

    GetImgExtraInfo.java

    Device Model Description=IEC 61966-2.1 Default RGB colour space - sRGB Viewing Conditions Description=Reference Viewing Condition in IEC61966-2.1 Viewing Conditions=view (0x76696577): 36 bytes ...

    C#读取JPEG图片的Exif信息

    /// This is default constructor of the EXIF class. /// public EXIF() { } /// /// This is base constructor of the EXIF class. /// public EXIF(string filePath) { _picture = Image.From...

    xml-rpc 1.5.1

    This is a better resolution of Bug 7376. * Turn off the default of automatically base64 encoding strings that can generate fatal errors in PHP's SAX parser. The automatic base64 encoding can be ...

    ImageMagick图片批量处理

    -units type the units of image resolution -verbose print detailed information about the image -view FlashPix viewing transforms -virtual-pixel method virtual pixel access method -weight type ...

    visual assist 10.5.1707

    Fixed global scope resolution operator (::) being changed to a single colon in Extract Method. (case=18573) 8021 Extract Method no longer offers to extract to source file when no source file is ...

    visual assist 1707破解

    * Fixed global scope resolution operator (::) being changed to a single colon in Extract Method. (case=18573) 8021 * Extract Method no longer offers to extract to source file when no source file is ...

    Computational Intelligence:An Introduction

    Chapter 8—CI systems 8.1. Introduction 8.2. Fuzzy encoding in evolutionary computing 8.2.1. Direct methods of fuzzy encoding 8.2.2. Weak encoding with fuzzy sets 8.3. Fuzzy crossover ...

    acpi控制笔记本风扇转速

    Added #pragma pack(8/4) to acobject.h to ensure that the structures in this header are always compiled as aligned. The ACPI_OPERAND_OBJECT has been manually optimized to be aligned and will not work ...

    WPTools.v6.29.1.Pro

    ******************************************* ************ WPTOOLS 6 History ************ ... also when scaled and also in high resolution rendering mode. + new code to draw dotted lines ...

    python3.6.5参考手册 chm

    PEP 529: Change Windows filesystem encoding to UTF-8 PEP 528: Change Windows console encoding to UTF-8 PEP 520: Preserving Class Attribute Definition Order PEP 468: Preserving Keyword Argument ...

    eac3to V3.17

    * added support for MKV "SRT/UTF8", "SRT/ASCII", "ASS" and "SSA" subtitles * increased some internal buffers to avoid AC3 overflow in the "thd ac3 joiner" * fixed: frame counting didn't work for MKV ...

    一本android的好书beginning android 2 和 源码

    ■Chapter 8: Getting Fancy with Lists Getting to First Base A Dynamic Presentation Better Stronger Faster Using convertView Using the Holder Pattern Making a List And Checking It Twice Adapting ...

    GifDecoder

    int inc = 8; int iline = 0; for (int i = 0; i ; i++) { int line = i; if (interlace) { if (iline >= ih) { pass++; switch (pass) { case 2 : iline = 4; break; case 3 : iline = 2; inc = 4; ...

    ZendFramework中文文档

    14.2.8. RealPath 14.2.9. StringToLower 14.2.10. StringToUpper 14.2.11. StringTrim 14.2.12. StripTags 14.3. 过滤器链 14.4. 编写过滤器 14.5. Zend_Filter_Input 14.5.1. Declaring Filter and ...

    Bochs - The cross platform IA-32 (x86) emulator

    In addition to the default Bochs method using the CTRL key and the middle mouse button there are now the choices: - CTRL+F10 (like DOSBox) - CTRL+ALT (like QEMU) - F12 (replaces win32 'legacyF12'...

    高清监控系统兼容所有网络摄像机

    Update method:=系统升级: Select...=浏览... Progress:=升级进度: Update begin=升级 Cancel=取消 Restart=重启设备 Recovery=恢复系统 WEB Update=网站升级 File Update=文件升级 Device is busing refuse update ...

    Objective-C 基础教程

    - **Dynamic Method Resolution(动态方法解析)**:探讨了Objective-C中动态方法解析的过程,即如何确定调用哪个方法实现。 - **Dot Syntax(点语法)**:说明了点语法在Objective-C中的使用,它是一种更简洁的...

    BURNINTEST--硬件检测工具

    - A printer to run the printer test, set-up as the default printer in Windows. - A CD ROM + 1 Music CD or Data CD to run the CD test. - A CD-RW to run the CD burn test. - A network connection and the ...

Global site tag (gtag.js) - Google Analytics