`
Java_Tony
  • 浏览: 20424 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

abstract and interface...

    博客分类:
  • Java
 
阅读更多
Java接口特性学习
在Java中看到接口,第一个想到的可能就是C++中的多重继承和Java中的另外一个关键字abstract。从另外一个角度实现多重继承是接口的功能之一,接口的存在可以使Java中的对象可以向上转型为多个基类型,并且和抽象类一样可以防止他人创建该类的对象,因为接口不允许创建对象。
interface关键字用来声明一个接口,它可以产生一个完全抽象的类,并且不提供任何具体实现。interface的特性整理如下:
1.        接口中的方法可以有参数列表和返回类型,但不能有任何方法体。
2.        接口中可以包含字段,但是会被隐式的声明为static和final。
3.        接口中的字段只是被存储在该接口的静态存储区域内,而不属于该接口。
4.        接口中的方法可以被声明为public或不声明,但结果都会按照public类型处理。
         (方法默认public)
5.        当实现一个接口时,需要将被定义的方法声明为public类型的,否则为默认访问类型,Java编译器不允许这种情况。
6.        如果没有实现接口中所有方法,那么创建的仍然是一个接口。
7.        扩展一个接口来生成新的接口应使用关键字extends,实现一个接口使用implements。
interface在某些地方和abstract有相似的地方,但是采用哪种方式来声明类主要参照以下两点:
1. 如果要创建不带任何方法定义和成员变量的基类,那么就应该选择接口而不是抽象类。
2. 如果知道某个类应该是基类,那么第一个选择的应该是让它成为一个接口,只有在必须要有方法定义和成员变量的时候,才应该选择抽象类。因为抽象类中允许存在一个或多个被具体实现的方法,只要方法没有被全部实现该类就仍是抽象类。
以上就是接口的基本特性和应用的领域,但是接口绝不仅仅如此,在Java语法结构中,接口可以被嵌套,既可以被某个类嵌套,也可以被接口嵌套。这在实际开发中可能应用的不多,但也是它的特性之一。需要注意的是,在实现某个接口时,并不需要实现嵌套在其内部的任何接口,而且,private接口不能在定义它的类之外被实现

Above,是摘抄的别人的总结,不过从我自身的实际经验来看,很少用到Abstract来定义行为的,多数是Interface来定义。当然上面说的接口的应用领域,应该还有其他方面,有待补充和实践...
分享到:
评论
1 楼 361010911 2011-07-18  
Abstract應用去實體類 還是一個不錯的選者 哈哈

相关推荐

    Guide to Scientific Computing in C++

    5 Blocks, Functions and Reference Variables . . . . . . . 5.1 Blocks . . . . . . . . . . . . . . . . . . . . . . . . 5.2 Functions . . . . . . . . . . . . . . . . . . . . . . . 5.2.1 Simple Functions ...

    NIST SP800-73-4.pdf

    The goals are addressed by specifying a PIV data model, card edge interface, and application programming interface. Moreover, this document enumerates requirements where the international integrated ...

    servlet2.4doc

    Adds a response header with the given name and date-value. addHeader(String, String) - Method in class javax.servlet.http.HttpServletResponseWrapper The default behavior of this method is to return ...

    coreJava240问与答面试题

    6) Difference between abstract class and interface ? ............................................................ 10 7) Why java is platform independent? .................................................

    Java Methods-Class Hierarchies and Interfaces.ppt

    public abstract void draw(Graphics g, boolean makeItFilled); } public class RoundBalloon extends Balloon { // ... @Override public void draw(Graphics g, boolean makeItFilled) { // 圆形气球的绘制...

    KUKA_FRI_from_WS_Proceedings_ICRA2010.pdf

    Abstract—The KUKA lightweight robot (LWR) provides many unique features for robotic researchers. To give full access to these features, a new interface was ...and first applications of the interface.

    SystemVerilog Reference Manual 3.1a(中英文版)+最新SV IEEE 标准

    2.3 Integer and logic literals ..................................................................................................................4 2.4 Real literals ......................................

    Unsteady Turbulent Simulation and Pressure fFluctuation Analysi for Centrifugal Pumps

    peak-to-peak values in the impeller are increasing,and reach the maximum value on the interface. In the casing flow passage,those values are about 7% of local static pressure except some ones near...

    The Base Standard of Application Binary Interface for the ARM Architecture.pdf

    This document describes the structure of the Application Binary Interface (ABI) for the ARM architecture, and links to the documents that define the base standard for the ABI for the ARM Architecture...

    python3.6.5参考手册 chm

    PEP 3333: Python Web Server Gateway Interface v1.0.1 Other Language Changes New, Improved, and Deprecated Modules email elementtree functools itertools collections threading datetime and time ...

    oriented.php.concepts.techniques.and.code

    PHP中的`interface`关键字用于声明接口,类通过`implements`关键字实现接口,确保遵循特定的行为约定。 **5. 抽象类(Abstract Classes)** 抽象类是不能被实例化的类,通常用来定义通用的行为,其目的是为了被其他...

    Java Swing, Second Edition

    The Abstract Window Toolkit (AWT) was part of the JDK from the beginning, but it really wasn't sufficient to support a complex user interface. It supported everything you could do in an HTML form and...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    - **Abstract Classes**: Abstract classes cannot be instantiated and are used as base classes for other classes. - **Interfaces**: Interfaces define contracts that classes must implement. This section ...

    SCJP6 Sun Certificated Programmer for Java 6 Study Guide (Exam 310-065) 英文原版

    - **Thread Creation**: Extending Thread class or implementing Runnable interface. - **Synchronization**: Preventing concurrent access to shared resources. - **Interrupts and Sleep**: Managing thread ...

    UEFI 2.8协议规范(英文)---UEFI_Spec_2_8_final.pdf

    complete abstract specification of the software-visible interface presented to the OS by the platform and firmware. Using this formal definition, a shrink-wrap OS intended to run on platforms ...

Global site tag (gtag.js) - Google Analytics