1. A class containing abstract methods is called an abstract class. If a class Contains one of more abstract methods, the class itself must be qualified(限定,修饰) as a abstract.
  2. If you inherit from an abstract class you want to make objects of the new type, you must provide method definitions for all the abstract methods in the base class. If you don't(and you may choose not to), then the derived class is also abstract, and the compiler will force you to qualify that class with the abstract keyword.
  3. It's possible to make a class abstract without including any abstract methods. This is useful when you've got a class in which it doesn't make sense to have any abstract methods, and yet you want to prevent any instances of that class.
  4. If you do inherit from a non-interface, you can inherit from only one. All the rest of the base elements must be interfaces. You place all the interface names after the implements keyword and separate them with commas. You can have as many interfaces ay you want. You can upcast to each interface, because each interface is an independent type.
  5. 如果你创建的新的类即继承了某个类同时又实现了某些接口,那么在类声明的时候,被继承的类必须是第一个出现的,然后才是接口(如果你不这样做的话,编译器就会报错)。
  6. Keep in mind that one of the core reasons for interfaces is: to upcast to more than one base type(and the flexibility that this provides). However, a second reason for using interfaces is the same as using an abstract base class: to prevent the client programmer from making an object of this class and to establish that it is only an interface. This brings up a question: Should you use an interface or an abstract class? If it's possible to create your base class without any method definitions or member variables, you should always prefer interfaces to abstract classes. In fact, if you know something is going to be a base class, you can consider making it an interface.
  7. Normally, you can use extends with only a single class, but extends can refer to multiple base interfaces when building a new interface(The interface names are simply separated with commas).
  8. 我们知道一个接口可以继承多个接口,也可以继承一个类同时又实现多个接口,如果它继承的多个接口(或者继承的类和实现的多个接口)中有同名同参但是返回值不同的方法时会出现什么情况呢?答案是会出现命名冲突,编译不通过,因为两个方法仅仅是返回值类型不同并不能构成重载。因此我们应当尽量避免在不同的接口中用两个相同的方法名,尤其是这两个接口常常被一个类一同实现的时候更应该额外注意这一点。
  9. 在Java SE5 之前,Java 中还没有enum 关键字,接口就成为提供一组常量值的一个简便的工具(在一些遗留的老代码中可能会看到这种情况)。接口中的成员变量,自动的是public static final 的。这些值被存储在静态存储区中。
  10. 许多人痴迷于接口的特性,总是想尽办法在一切地方应用它,其实这是一种过早优化(万恶之源:P)。An appropriate guideline is to prefer classes to interfaces. Start with classes, and if it becomes clear that interfaces are necessary, then refactor. Interfaces are a great tool, but they can easily be overused.

TIJ——Chapter Nine:Interfaces的更多相关文章

  1. TIJ——Chapter Eleven:Holding Your Objects

    Java Provides a number of ways to hold objects: An array associates numerical indexes to objects. It ...

  2. TIJ——Chapter Two:Everything Is an Object

    If we spoke a different language, we would perceive a somewhat different world. Ludwig Wittgenstein( ...

  3. TIJ——Chapter One:Introduction to Objects

    ///:~容我对这个系列美其名曰"读书笔记",其实shi在练习英文哈:-) Introduction to Objects Object-oriented programming( ...

  4. TIJ——Chapter Fourteen:Type Information

    Runtime type information(RTTI) allows you to discover and use type information while a program is ru ...

  5. TIJ——Chapter Twelve:Error Handling with Exception

    Exception guidelines Use exceptions to: Handle problems at the appropriate level.(Avoid catching exc ...

  6. TIJ——Chapter Ten:Inner Classes

    先提纲挈领地来个总结: 内部类(Inner Class)有四种: member inner class,即成员内部类.可以访问外部类所有方法与成员变量.生成成员内部类对象的方法:OuterClass. ...

  7. TIJ——Chapter Eight:Polymorphism

    The twist |_Method-call binding Connecting a method call to a method body is called binding. When bi ...

  8. TIJ——Chapter Seven:Reusing Classes

    Reusing Classes 有两种常用方式实现类的重用,组件(在新类中创建存在类的对象)和继承. Composition syntax Every non-primitive object has ...

  9. TIJ——Chapter Five:Initialization & Cleanup

    Method overloading |_Distinguishing overloaded methods If the methods hava the same name, how can Ja ...

随机推荐

  1. C# 制作ActiveX控件并添加到网页

    1.创建ActiveX控件——按钮 2.定义一个接口,并在控件中实现 3.部署安装 4.CAB打包 5.添加到网页中进行测试 一. 创建ActiveX控件——按钮 1.新建一个Window窗体控件库项 ...

  2. SQLSTATE[HY000] [2002] 乱码

    string(59) "SQLSTATE[HY000] [2002] ����Ŀ����������ܾ����޷����ӡ� " 实际意思是:SQLSTATE[HY000] [20 ...

  3. Python eval()函数的用法

    Python eval()函数的用法 eval(str)函数很强大,官方解释为:将字符串str当成有效的表达式来求值并返回计算结果.所以,结合math当成一个计算器很好用. eval()函数常见作用有 ...

  4. Tarjan求LCA(离线)

    基本思想 把要求的点对保存下来,在dfs时顺带求出来. 方法 将每个已经遍历的点指向它回溯的最高节点(遍历它的子树时指向自己),每遍历到一个点就处理它存在的询问如果另一个点已经遍历,则lca就是另一个 ...

  5. ASP.NET Core修改IOC为Autofac

    如下是我为了了解如何更换ASP.NET Core中的IOC而查找的文章,如果大家英文OK的,可以直接前往阅读,同时也已经有简单的github例子供大家参考. 参考文章: ASP.NET Core文档: ...

  6. maven-home

    E:/Soft/Maven/apache-maven-3.3.3 E:\Soft\Maven\apache-maven-3.3.3\conf\settings.xml E:\DellWork\Mave ...

  7. laravel 下载报错:Unable to guess the mime type as no guessers are available

    在使用laravel的download()函数实现下载功能时,报错如下:Unable to guess the mime type as no guessers are available (Did ...

  8. 锋利的jQuery学习笔记之jQuery选择器

    在介绍jQuery选择器之前,先简单介绍一下CSS选择器---> 一.CSS选择器 常见的CSS选择器有以下几种: 选择器 语法 描述 示例 标签选择器 E{CSS规则} 以文档元素为选择符 t ...

  9. linux如何将分组权限置为空

    两种方法 方法一:使用-符号 chmod g=- monkey.py#可以单独指定一个 方法二:简写方式,用0表示 chmod 740 monkey.py#必须同时指定三个的权限

  10. Lab2 内存管理(实现细节)

    lab2 中的变动 bootloader 的入口发生了改变 bootloader不像lab1那样,直接调用kern_init函数,而是先调用位于lab2/kern/init/entry.S中的kern ...