暗黑二天梯:(Java)谁能给个“面向接口编程”的例子?

来源:百度文库 编辑:高考问答 时间:2024/04/29 01:39:46
要能在Eclipse下运行通过。感激涕零!
唉,只要有程序就可以了,不需要运行通过。

接口是写在一个.Java文件中,还是写在不同的.Java文件中呢?

很简单啊,我给你一个java类库里的接口怎样啊?是一个经常用到的MouseListener:
/*
* @(#)MouseListener.java 1.17 03/12/19
*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

package java.awt.event;

import java.util.EventListener;

/**
* The listener interface for receiving "interesting" mouse events
* (press, release, click, enter, and exit) on a component.
* (To track mouse moves and mouse drags, use the
* <code>MouseMotionListener</code>.)
* <P>
* The class that is interested in processing a mouse event
* either implements this interface (and all the methods it
* contains) or extends the abstract <code>MouseAdapter</code> class
* (overriding only the methods of interest).
* <P>
* The listener object created from that class is then registered with a
* component using the component's <code>addMouseListener</code>
* method. A mouse event is generated when the mouse is pressed, released
* clicked (pressed and released). A mouse event is also generated when
* the mouse cursor enters or leaves a component. When a mouse event
* occurs, the relevant method in the listener object is invoked, and
* the <code>MouseEvent</code> is passed to it.
*
* @author Carl Quinn
* @version 1.17, 12/19/03
*
* @see MouseAdapter
* @see MouseEvent
* @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/mouselistener.html">Tutorial: Writing a Mouse Listener</a>
* @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
*
* @since 1.1
*/
public interface MouseListener extends EventListener {

/**
* Invoked when the mouse button has been clicked (pressed
* and released) on a component.
*/
public void mouseClicked(MouseEvent e);

/**
* Invoked when a mouse button has been pressed on a component.
*/
public void mousePressed(MouseEvent e);

/**
* Invoked when a mouse button has been released on a component.
*/
public void mouseReleased(MouseEvent e);

/**
* Invoked when the mouse enters a component.
*/
public void mouseEntered(MouseEvent e);

/**
* Invoked when the mouse exits a component.
*/
public void mouseExited(MouseEvent e);
}
接口与类的写法差不多,这个接口放在MouseListener.java(称为一个编辑单元)里.

开发系统时, 主体构架使用接口, 接口构成系统的骨架.这样就可以通过更换实现接口的类来更换系统的实现.
比如在eclipse中, 功能都是通过插件方式实现的, 这些插件的开发就是通过面向接口的编程方式, 你开发的插件必须实现eclipse规定的某个接口, 这样才能将插件融入eclipse中.比如要实现一个在工具栏里显示的按钮, 你就需要创建一个plugin project创建一个实现IWorkbenchWindowActionDelegate接口的类.

同样在SharpDevelop这样的插件式开发环境中, 要想自己开发插件插入系统就需要了解ITreeNode接口.

这些面向接口开发的IDE的架构基本概念可以表述成下面的代码:
public class InterfaceOriented {
public static void main(String[] args){
IHelloPlugin[] pluginTree = new IHelloPlugin[2];
pluginTree[0] = new A();
pluginTree[1] = new B();
for(IHelloPlugin plugin : pluginTree){
plugin.run();
}
}
}

interface IHelloPlugin{
void run();
}

class A implements IHelloPlugin{
public void run() {
System.out.println("Hello from A");
}
}

class B implements IHelloPlugin{
public void run() {
System.out.println("Hello from B");
}
}