成都创新互联网站制作重庆分公司

java接口开发代码 java接口代码实例

(Java)谁能给个“面向接口编程”的例子?

分类: 电脑/网络 程序设计 其他编程语言

创新互联-专业网站定制、快速模板网站建设、高性价比汉阴网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式汉阴网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖汉阴地区。费用合理售后完善,10多年实体公司更值得信赖。

问题描述:

要能在Eclipse下运行通过。感激涕零!

解析:

很简单啊,我给你一个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 ponent.

* (To track mouse moves and mouse drags, use the

* codeMouseMotionListener/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 codeMouseAdapter/code class

* (overriding only the methods of interest).

* P

* The listener object created from that class is then registered with a

* ponent using the ponent's codeaddMouseListener/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 ponent. When a mouse event

* occurs, the relevant method in the listener object is invoked, and

* the codeMouseEvent/code is passed to it.

*

* @author Carl Quinn

* @version 1.17, 12/19/03

*

* @see MouseAdapter

* @see MouseEvent

* @see a href="java.sun/docs/books/tutorial/post1.0/ui/mouselistener"Tutorial: Writing a Mouse Listener/a

* @see a href="awl/cp/javaseries/jcl1_2"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 ponent.

*/

public void mouseClicked(MouseEvent e);

/**

* Invoked when a mouse button has been pressed on a ponent.

*/

public void mousePressed(MouseEvent e);

/**

* Invoked when a mouse button has been released on a ponent.

*/

public void mouseReleased(MouseEvent e);

/**

* Invoked when the mouse enters a ponent.

*/

public void mouseEntered(MouseEvent e);

/**

* Invoked when the mouse exits a ponent.

*/

public void mouseExited(MouseEvent e);

}

接口与类的写法差不多,这个接口放在MouseListener.java(称为一个编辑单元)里.

java接口编程

//Muitiplication接口

public interface Muitiplication{

double mult(double a, double b);

}

//MuitiplicationImpl实现类

public class MuitiplicationImpl implements Muitiplication{

public double mult(double a, double b){

return a * b;

}

}

//MuitiplicationImpls实现类

public class MuitiplicationImpls implements Muitiplication{

public double mult(double a, double b){

return a + b;

}

}

public static void main(args[]){

MuitiplicationImpl m = new MuitiplicationImpl();

System.out.println(m.mult(1.001,1.001));

MuitiplicationImpls ms = new MuitiplicationImpls();

System.out.println(ms.mult(1.001,1.001));

}

java创建接口编程怎么写代码

interface Bike{

public void radio();

}

interface Car{

public void tv();

}

interface Dt{

public void music();

}

public class InterfaceDemo implements Bike,Car,Dt{

public void radio() {

System.out.println("可以听广播");

}

public void tv() {

System.out.println("可以看电视");

}

public void music() {

System.out.println("可以听音乐");

}

public static void main(String args[])

{

InterfaceDemo m=new InterfaceDemo();

m.radio();

m.tv();

m.music();

}

}

用java编写一个程序用到接口,能用到接口概念的就行

Display.java   接口代码如下:

public interface Display {

public void dis();

}

接口的实现类DisplayImpl.java    代码如下:

public class DisplayImpl implements Display {

@Override

public void dis() {

// TODO Auto-generated method stub

System.out.println("输出方法");

}

public static void main(String[] args) {

new DisplayImpl().dis();

}

}


当前标题:java接口开发代码 java接口代码实例
URL分享:http://cxhlcq.com/article/ddehiop.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部