TIJ——Chapter Nine:Interfaces
- 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.
- 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.
- 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.
- 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.
- 如果你创建的新的类即继承了某个类同时又实现了某些接口,那么在类声明的时候,被继承的类必须是第一个出现的,然后才是接口(如果你不这样做的话,编译器就会报错)。
- 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.
- 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).
- 我们知道一个接口可以继承多个接口,也可以继承一个类同时又实现多个接口,如果它继承的多个接口(或者继承的类和实现的多个接口)中有同名同参但是返回值不同的方法时会出现什么情况呢?答案是会出现命名冲突,编译不通过,因为两个方法仅仅是返回值类型不同并不能构成重载。因此我们应当尽量避免在不同的接口中用两个相同的方法名,尤其是这两个接口常常被一个类一同实现的时候更应该额外注意这一点。
- 在Java SE5 之前,Java 中还没有enum 关键字,接口就成为提供一组常量值的一个简便的工具(在一些遗留的老代码中可能会看到这种情况)。接口中的成员变量,自动的是public static final 的。这些值被存储在静态存储区中。
- 许多人痴迷于接口的特性,总是想尽办法在一切地方应用它,其实这是一种过早优化(万恶之源: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的更多相关文章
- TIJ——Chapter Eleven:Holding Your Objects
Java Provides a number of ways to hold objects: An array associates numerical indexes to objects. It ...
- TIJ——Chapter Two:Everything Is an Object
If we spoke a different language, we would perceive a somewhat different world. Ludwig Wittgenstein( ...
- TIJ——Chapter One:Introduction to Objects
///:~容我对这个系列美其名曰"读书笔记",其实shi在练习英文哈:-) Introduction to Objects Object-oriented programming( ...
- TIJ——Chapter Fourteen:Type Information
Runtime type information(RTTI) allows you to discover and use type information while a program is ru ...
- TIJ——Chapter Twelve:Error Handling with Exception
Exception guidelines Use exceptions to: Handle problems at the appropriate level.(Avoid catching exc ...
- TIJ——Chapter Ten:Inner Classes
先提纲挈领地来个总结: 内部类(Inner Class)有四种: member inner class,即成员内部类.可以访问外部类所有方法与成员变量.生成成员内部类对象的方法:OuterClass. ...
- TIJ——Chapter Eight:Polymorphism
The twist |_Method-call binding Connecting a method call to a method body is called binding. When bi ...
- TIJ——Chapter Seven:Reusing Classes
Reusing Classes 有两种常用方式实现类的重用,组件(在新类中创建存在类的对象)和继承. Composition syntax Every non-primitive object has ...
- TIJ——Chapter Five:Initialization & Cleanup
Method overloading |_Distinguishing overloaded methods If the methods hava the same name, how can Ja ...
随机推荐
- BZOJ 4554: [Tjoi2016&Heoi2016]游戏
Time Limit: 20 Sec Memory Limit: 128 MB Submit: 951 Solved: 572 [Submit][Status][Discuss] Descriptio ...
- netbeans性能分析文件保存位置
C:\Users\Administrator\AppData\Roaming\NetBeans\8.2\config\HTTPMonitor 分析完,记得把文件删除,不然系统盘要满了
- 利用javafx编写一个时钟制作程序
1.首先创建一个时钟类,用于编写时钟的各种特有属性 package javaclock; /** * * @author admin */import java.util.Calendar;impor ...
- canvas旋转图片
canvas旋转图片 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 2019.9.29 csp-s模拟测试55 反思总结
不咕咕咕是一种美德[大雾] 头一次体会到爆肝写题解??? 这次考试我们没赶上,是后来掐着时间每个人自己考的.我最后的分数能拿到152…熟悉的一题AC两题爆炸. 强烈吐槽出题人起名走心 T1联: 发现每 ...
- 洛谷P1315 [NOIP2011提高组Day2T3] 观光公交
P1315 观光公交 题目描述 风景迷人的小城Y 市,拥有n 个美丽的景点.由于慕名而来的游客越来越多,Y 市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第 0 分钟出现在 1号 ...
- Leetcode633.Sum of Square Numbers平方数之和
给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c. 示例1: 输入: 5 输出: True 解释: 1 * 1 + 2 * 2 = 5 示例2: 输入: 3 ...
- ant详细介绍
Ant的概念 可能有些读者并不连接什么是Ant以及入可使用它,但只要使用通过Linux系统得读者,应该知道make这个命令.当编译Linux内核及一些软件的源程序时,经常要用这个命令.Make命令 ...
- angular依赖注入(3) —— 的提供商
一.注册商必须写在NgModel当中,不然只能对当前组件注入 二.写法: 1.令牌:providers:[Logger,Data] 2.供应商定义对象:providers:[{provide:Logg ...
- mac Chrome 快捷键
从windows迁移到mac,会发现快捷键真的变了,这里先吹一下mac OS天下第一 另外没有bar确实也提高了效率,这一点还是值的 其实程序员最常用的是检查,windows下是F12,默认F12会被 ...