Java Knowledge series 5
Interface from user, not from implementor.(DIP)
Interface-Oriented Programming.
Interface or Abstract function or Abstract class
- Java专门提供了一种机制,名为“抽象方法”(same as abstraction)。它属于一种不完整的方法,只含有一个声明declaration,没有方法主体 body of definition。下面是抽象方法声明时采用的语法:abstract void X();包含了抽象方法的一个类叫作“抽象类”abstract class with one or more abstract functions。
- 如果一个类里包含了一个或多个抽象方法,类就必须指定成abstract(抽象)。
- 如果从一个抽象类继承,而且想生成新类型的一个对象,就必须为基础类中的所有抽象方法提供方法定义。如果不这样做(完全可以选择不做),则衍生类derived也会是抽象的,而且编译器会强迫我们用abstract关键字标志那个类的“抽象”本质。
- 即使不包括任何abstract方法,亦可将一个类声明成“抽象类”。如果一个类没必要拥有任何抽象方法,而且我们想禁止那个类的所有实例,这种能力就会显得非常有用。User can’t create a instance of abstract class.
- “interface”(接口pure abstract class)关键字使抽象的概念更深入了一层。我们可将其想象为一个“纯”抽象类。它允许创建者规定一个类的基本形式:方法名、自变量列表以及返回类型,但不规定方法主体。接口也包含了基本数据类型的数据成员,但它们都默认为static和final。
- 接口只提供一种形式,并不提供实施的细节。接口这样描述自己:“对于实现我的所有类,看起来都应该象我现在这个样子”。
- It is impossible to create a instance of abstract class or interface.
- 这便是接口的全部含义。所以我们常把接口用于建立类和类之间的一个“协议”。有些面向对象的程序设计语言采用了一个名为“protocol”(协议)的关键字,它做的便是与接口相同的事情。
- 为创建一个接口,请使用interface关键字,而不要用class。与类相似,我们可在interface关键字的前面增加一个public关键字(但只有接口定义于同名的一个文件内);或者将其省略,营造一种“友好的”状态。为了生成与一个特定的接口(或一组接口)相符的类,要使用implements(实现)关键字。我们要表达的意思是“接口看起来就象那个样子,这儿是它具体的工作细节”。
- Public interface TestListener{}
- Public interface Receiver{}
- public abstract class BaseTest implements TestListener,Receiver{}
- public class DerivedTest extends BaseTest implements Clonable, Runnable, Comparable{}
- 由于接口根本没有具体的实施细节——也就是说,没有与存储空间与“接口interface”关联在一起——所以没有任何办法可以防止多个接口合并到一起。这一点是至关重要的,因为我们经常都需要表达这样一个意思:“x从属于a,也从属于b,也从属于c”。在C++中,将多个类合并到一起的行动称作“多重继承”multiple inheritance,而且操作较为不便,因为每个类都可能有一套自己的实施细节。在Java中,我们可采取同样的行动,但只有其中一个类拥有具体的实施细节。
- 如果确实想从一个非接口继承,那么只能从一个继承。剩余的所有基本元素都必须是“接口”。我们将所有接口名置于implements关键字的后面,并用逗号分隔它们。
- 利用继承技术,可方便地为一个接口添加新的方法声明,也可以将几个接口合并成一个新接口。
- 由于置入一个接口的所有字段都自动具有static和final属性 in interface,所以接口是对常数值进行分组的一个好工具,它具有与C或C++的enum非常相似的效果。
- 接口中定义的字段会自动具有static和final属性。它们不能是“空白final”,但可初始化成非常数表达式。由于字段是static的,所以它们会在首次装载类之后、以及首次访问任何字段之前获得初始化。
- The interface, remember, automatically makes all of its members public.
- Interface and inner classes provide more sophisticated ways to organize and control the objects in your systems.
- The abstract keyword, allows you to create one or more methods in a class that have no definitions- you provide part of the interface without providing a corresponding implementation, which is created by inheritors.
- The interface keyword produces a completely abstract class, one that provides no implementation at all. You will learn that the interface is more than just an abstract class taken to the extreme, since it allows you to perform a variation on C++’s “multiple inheritance” by creating a class that can be upcast to more than one base type.
- interface in COM is defined as struct to keep a lot of ‘public’ lines out of the interfaces.
- The interface in Java takes the abstract concept one step further. You could think of it as a “pure” abstract class (like depicted in C++).
- Interface allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies (which shall be given in concreate class that implements this interface).
- An interface can also contain fields, but these are implicitly static and final (pertain to all object of the class and never be changed).
- An interface provides only a form, but no implementation. (What placement means).
- An interface says, “This is what all classes that implement this particular interface will look like.”
- The interface is used to establish a “protocol” between classes. (some object-oriented programming languages have a keyword called protocol to do the same thing.)
- OOP MDD ( Model-Driven Design) IOD ( Inteface-Oriented Design) DDD (Domain-Driven Design) PDD ( Pattern-Oriented Design)
- SOA:Service-Oriented Architecture
- To create an interface, use the interface keyword instead of the class keyword. Like a class, you can add the public keyword before the interface keyword (but only if that interface is defined in a file of the same name) or leave it off to give package access, so that it is only usable within the same package.
- To make a class that conforms to a particular interface (or group of interfaces), use the implements keyword, which says, “The interface is what it looks like, but now I am going to say how it works.”
- You can choose to explicitly declare the method as public in an interface, but they are public even if you don’t do that. The methods in interface have a defacult access control “public”.
- When you implement an interface, the methods from the interface must be defined as public. Otherwise, they would defacult to package access (friendly), and you had be reducing the accessibility of a method during inheritance, which is not allowed by the Java compiler.
- The variants in interface are static and final. Which means they are compile-time constant.
- This is the intent: Each approach gives the programmer different control over the way objects are created and used.
- “Multiple inheritance” in Java: The interface is not simply a “more pure” form of abstract class. It has a higher purpose than that. (So same as interface in COM). Because an interface has no implementation at all, that is, there is no storage associated with an interface-there is nothing to prevent many interfaces from being combined.
- Interface just look like a placement for the concreate class in order to make JVM know what methods the class must and have to have.
- In a derived class, you are not forced to have a base class that is either an abstract or ‘concrete’ ( one with no abstract methods). If you do inherit from a non-inferface, 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 as you want; each one becomes an independent type that you can upcast to.
- In concrete class with all definitions of methods of interface provided, all methods which are in implemented interfaces shall be provided definition (body), no matter the definition of method is gaven in this class or its base class. Just having one definition is OK.
- A 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.
- An interface gives you the benefits of an abstract class and the benefits of an interface, so 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, your first choice should be to make it an interface, and only if you’re forced to have method definitions or member variables should you change to an abstract class, or if necessary a concrete class.
- Name collisions when combining interfaces: if a few of interface has the method with same name, but different argument type or different return type, what will happen?
- The difficulty occurs because overriding, implementation, and overloading get unpleasantly mixed together, and overloaded methods can not differ only by return type. When the last two lines are uncommented, the error messages it all:
- Using the same method names in different interfaces that are intended to be combined generally causes confusion in the readability of the code, as well. Strive to avoid it.
- Extending an interface with inheritance: You can easily add new method declarations to an interface by using inheritance, and you can also combine several interfaces into a new interface with inheritance. In both cases you get a new interface.
- Grouping constants: because any fields you put into an interface are automatically static and final, the interface is a convenient tool for creating groups of constant values, much as you would with an enum in C or C++.
- Notice the Java style of using all uppercase letters ( with underscores to separate multiple words in a single identifier) for static finals that have constant initializers.
- The fields in an interface are automatically public, so it is unnecessary to specify that.
- You can use the constants from outside the package by importing c08.* or c08.Months just as you would with any other package, and referencing the values with expressions like Months.JANUARY. Of course, what you get is just an int, so there isn’t the extra type safety that C++’s enum has, but this (commonly used) technique is certainly an improvement over hard coding numbers into your programs. (That approach is often referred to as using “magic numbers,” and it produces very difficult-to-maintain code.)
- Initializing fields in interfaces: Fields defined in interfaces are automatically static and final. These can not be “blank finals”, but they can be initialized with non-constant expressions. Since the fields are static, they are initialized when the class is first loaded, which happed when any of the fields are accessed for the first time.
- The fields, of course, are not part of the interface but instead are stored in the static storage area for that interface.
- Nesting interfaces: interfaces may be nested within classes and within other interfaces.
Java Knowledge series 5的更多相关文章
- Java Knowledge series 4
JVM & Bytecode Has-a or Is-a relationship(inheritance or composition) 如果想利用新类内部一个现有类的特性,而不想使用它的接 ...
- Java Knowledge series 7
Pepole who make a greate contribution on common libaraies deserve our respect. Component(Widget) / S ...
- Java Knowledge series 3
JVM & Bytecode Abstract & Object Object in Java (1) 所有东西都是对象object.可将对象想象成一种新型变量:它保存着数据,但可要求 ...
- Java Knowledge series 2
JVM Analysis & Design The object-oriented paradigm is a new and different way of thingking about ...
- Java Knowledge series 1
Programming language evolves always along with Compiler's evolvement JVM as Additional Indirection I ...
- Java SE series:2. enhance your java basis! [doc chm: jdk6api Chinese reference]
1. javaee(Web) and Android 2. how to use eclipse and break point debuging in eclipse, as to java web ...
- Java SE series:1. environment configure and Hello world! [We use compiler and packager to create an application!]
1. cli (command line interface) and gui (graphic user interface) use javahome path, search classpath ...
- C++ Knowledge series 1
Programming language evolves always along with Compiler's evolvement. 1. The C++ Object Model: Strou ...
- C++ Knowledge series STL & Const
Thank to the pepole who devote theirself to the common libs. STL(http://www.cplusplus.com/reference/ ...
随机推荐
- linux read和write
int write(int fd, const void *buf, size_t count); 如果是堵塞fd,则返回值是count,或者是0,-1: 如果是非堵塞fd,则返回值是写入的字节数,或 ...
- 黑马学习连接池 druid JdbcTemplate c3p0 池技术
package cn.itcast.jdbctemplate; import org.junit.Test; import org.springframework.jdbc.core.BeanProp ...
- 下载GitHub仓库的某个子文件夹
http://downgit.zhoudaxiaa.com/#/home
- matplotlib学习笔记(三)
柱状图 柱状图用其每根柱子的长度表示值的大小,它们通常用来比较两组或多组值.下面的程序从文件中读入中国人口的年龄分布数据,并使用柱状图比较男性和女性的年龄分布. import numpy as np ...
- PS常用快捷键大全
察看图像类别 说明:: --- Shift键 : --- 空格键 *--- 在Imageready中不适用 § --- 只在Imageready中可用 动作 结果 双击工具箱::或Ct ...
- centos7博客的基础搭建(LNMP)
linux [root@zbb wordpress]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) nginx echo ...
- WebService、WCF、WebAPI、MVC的区别
Web Service 1.它是基于SOAP协议的,数据格式是XML 2.只支持HTTP协议 3.它不是开源的,但可以被任意一个了解XML的人使用 4.它只能部署在IIS上 WCF 1.这个也是基于S ...
- C:foreEach
c:forEach用法 <c:foreach>用法 <c:foreach>类似于for和foreach循环 以下是我目前见过的用法:1.循环遍历,输出所有的元素.& ...
- struts1学习
转载:https://blog.csdn.net/toyouheart/article/details/4509466
- python中bytes类型转换为str类型
使用的原因:基于URL解析报文的时候,要使用str类型,但是提供的确实bytes类型,报错: TypeError: must be str, not bytes 所以就把bytes类型转换为str类型 ...