Java接口与抽象类研究
来源:互联网 发布:水泵选型软件 编辑:程序博客网 时间:2024/06/16 14:52
简介
Java抽象类
有时,我们希望通过通用接口来操作一系列有继承关系的类。为此,Java提供了一个叫做抽象方法的机制,这种方法是不完整的,仅有声明而没有方法主体。下面是抽象方法所采用的语法:
abstract void f();包含抽象方法的类叫做抽象类。如果一个类包含一个或多个抽象方法,该类必须被限定为抽象的。
抽象类不允许实例化;如果从一个抽象类继承,并想创建该新类的对象,那么就必须为基类中的所有抽象方法提供方法的定义。如果不这样做,那么导出的类仍然为抽象类,且编译器会强制用abstract关键字来限定这个类。
创建抽象类和抽象方法非常有用,因为它们可以使类的抽象性明确起来,并告诉用户和编译器打算怎么来使用它们。抽象类还是很有用的重构工具,因为它们使我们可以很容易的将公共方法沿着继承层次结构向上移动。
Java接口
什么是接口
public interface Runnable() { public abstract void run();}Java接口本身没有任何实现,且接口方法只能是抽象的和公开的,Java接口不能有构造函数。Java接口可以定义域,但只能是public、静态和final的域。
接口与类的区别
接口与抽象类
什么是接口继承
接口作用
/* * Copyright (c) 2002-2006 by OpenSymphony * All rights reserved. */package com.opensymphony.xwork2;/** * All actions may implement this interface, which exposes * the execute() method. However, as of XWork 1.1, this is * not required and is only here to assist users. You are * free to create POJOs that honor the same contract * defined by this interface without actually implementing * the interface. */public interface Action { /** * The action execution was successful. Show result * view to the end user. */ public static final String SUCCESS = "success"; /** * The action execution was successful but do not * show a view. This is useful for actions that are * handling the view in another fashion like redirect. */ public static final String NONE = "none"; /** * The action execution was a failure. * Show an error view, possibly asking the * user to retry entering data. */ public static final String ERROR = "error"; /** * The action execution require more input * in order to succeed. * This result is typically used if a form * handling action has been executed so as * to provide defaults for a form. The * form associated with the handler should be * shown to the end user. * <p/> * This result is also used if the given input * params are invalid, meaning the user * should try providing input again. */ public static final String INPUT = "input"; /** * The action could not execute, since the * user most was not logged in. The login view * should be shown. */ public static final String LOGIN = "login"; /** * Where the logic of the action is executed. * * @return a string representing the logical result of the execution. * See constants in this interface for a list of standard result values. * @throws Exception thrown if a system level exception occurs. * Application level exceptions should be handled by returning * an error value, such as Action.ERROR. */ public String execute() throws Exception;}
/** * Provides a default implementation for the most common actions. * See the documentation for all the interfaces this class implements for more detailed information. */public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable {...... public void setActionMessages(Collection messages) { validationAware.setActionMessages(messages); } public Collection getActionMessages() { return validationAware.getActionMessages(); }......}因此,接口提供了关联以及方法调用上的可插入性,软件系统的规模越大,生命周期越长,接口的重要性就越大。接口使得软件系统在灵活性和可扩展性、可插入性方面得到保证。
接口与抽象类对比
一个没有抽象方法的抽象类从功能上看与接口相似。然而,需要注意如下区别:
1. 一个类可以实现多个接口,但只能继承最多一个抽象类;interface作为一个极度抽象的类,允许人们通过创建一个能够被向上转型为多种基类的类型,来实现某种类似多重继承变种的特性。
2. 抽象类可以包含具体的方法;接口的所有方法都是抽象的
3. 抽象类可以声明和使用域;接口则不能,但可以声明static和final域作为常量(这些域隐式地是static和final的。在JDK1.5引入enum类型后,通常不建议在接口中声明常量,而是尽量将具有分组类别的常量定义为enum类型)
4. 抽象类中的方法可以是public、protected、private或者默认的package;接口的方法都是public的
5. 抽象类可以定义构造函数;接口则不能
Java设计师应当主要使用Java接口和抽象Java类将软件单位与内部和外部耦合起来。换而言之,应当使用Java接口和抽象类而非具体类进行变量的类型声明、参量的类型声明、方法的返回类型声明以及数据类型的转换等。当然,一个更好的做法是仅仅实现Java接口,而不是使用抽象Java类来做上面的这些工作。在理想的情况下,一个具体Java类应当只实现Java接口和抽象类中声明过的方法,而不应当给出多余的方法。
参考资料
- Java接口与抽象类研究
- 抽象类 与 接口 研究
- Java:接口与抽象类
- JAVA 抽象类与接口
- Java抽象类与接口
- java 抽象类与接口
- JAVA接口与抽象类
- JAVA 接口与抽象类
- java抽象类与接口
- Java接口与抽象类
- Java抽象类与接口
- java抽象类与接口
- java抽象类与接口
- java:抽象类与接口
- java 抽象类 与 接口
- Java抽象类与接口
- java 接口与抽象类
- Java抽象类与接口
- assert函数
- hdu 1698
- poj 2509 简单模拟(烟屁换烟)
- 启动 Eclipse 弹 Failed to load the JNI shared library jvm.dll
- 玩转大数据系列之Apache Pig如何与Apache Solr集成(二)
- Java接口与抽象类研究
- treap启发式合并(BZOJ2809)||splay||左偏树
- c与c++中struct区别
- 1. jdbc一般事务步骤
- class demo
- 2015-03-03-Numpy(2)
- iOS8扩展插件开发配置
- windows7 cmd telnet不是内部或外部命令的问题
- Java项目经验——程序员成长的关键