zoukankan      html  css  js  c++  java
  • Interfaces

    阅读Java的官方Doc,总结如下。

    What is Interface

    An interface is a reference type, similar to a class, that can contain only

    1. constants (implicitly public, static, final)
    2. method signatures (no method body, no braces)
    3. default methods (has method body)
    4. static methods (has method body)
    5. nested types

    Keypoints

    Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.

    A class can implement multi interfaces.

    An interface can extend multi interface.

    All abstract, default, and static methods in an interface are implicitly public, so you can omit the public modifier.

    Default Methods

    A pretty clear application scenario here.

    Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older versions of those interfaces.

    When you extend an interface that contains a default method, you can do the following:

    1. Not mention the default method at all, which lets your extended interface inherit the default method.
    2. Redeclare the default method, which makes it abstract.
    3. Redefine the default method, which overrides it.

    Special Case

    Look at following codes. ContentVisitor is an interface.

    private final ContentVisitor visitor = new ContentVisitor() {
        public void onStartDocument() {
            throw new IllegalStateException();
        }

    Did we create an instance of interface?

    No. We can't instantiate an interface. Actually, what these codes do is to define an anonymous class that implements the interface, and instantiate that class.

    It's the same with following codes.

    private final class AnonymousContentVisitor implements ContentVisitor {
        public void onStartDocument() {
            throw new IllegalStateException();
        }
    }
    
    private final ContentVisitor visitor = new AnonymousContentVisitor();
  • 相关阅读:
    C#.NET中的ToString()数字格式化
    Entity Framework实现属性映射约定
    CentOS6.3安装MySQL5.5
    Xshell传输文件
    centos直接yum安装nginx
    winform 开发中 把耗时操作 封装起来 异步执行(.net 4.0)
    c# action<> func<> 这2个委托怎么用和理解
    git@oschina使用入门(图形界面版)
    Linux任务前后台的切换
    linux 常用命令,开发记住这些基本能够玩转linux
  • 原文地址:https://www.cnblogs.com/ireneyanglan/p/4949626.html
Copyright © 2011-2022 走看看