java接口对接开发实例(java对外接口开发实例)

网友投稿 495 2023-03-15


本篇文章给大家谈谈java接口对接开发实例,以及java对外接口开发实例对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。 今天给各位分享java接口对接开发实例的知识,其中也会对java对外接口开发实例进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

求java用接口创建对象的实例

import java.lang.Math;
public class N20{
public static void main(String[] args){

Rectangle r1=new Rectangle(3,5); //实例化对象java接口对接开发实例,使用所在类方法
System.out.println("(Rectangle) Diagonal="+r1.getDiagonal());
System.out.println("(Rectangle) Are="+r1.are());

Square s1=new Square(2);
System.out.println("(Square) Diagonal="+s1.getDiagonal());
System.out.println("(Square) Are="+s1.are());
/*
EqualDiagonal e1=new Rectangle(3,5); //利用接口
System.out.println("(Rectangle) Diagonal="+e1.getDiagonal());
Rectangle r1=new Rectangle(3,5);
System.out.println("(Rectangle) Are="+r1.are());
EqualDiagonal e2=new Square(2); //利用接口
System.out.println("(Square) Diagonal="+e2.getDiagonal());
Rectangle r2=new Square(2); //利用多态
System.out.println("(Square) Are="+r2.are());
*/
}
}
class Rectangle implements EqualDiagonal{
protected double side1;
protected double side2;

Rectangle(double side1,double side2){
this.side1=side1;
this.side2=side2;
}

public double are(){
return side1*side2;
}
public double getDiagonal(){
return Math.sqrt(side1*side1+side2*side2);
}
}
class Square extends Rectangle implements EqualDiagonal{
Square(double side){
super(side,side); //父类构造方法需要2个参数
}
public double are(){
return super.are();
}
public double getDiagonal(){
return Math.sqrt(2)*side1;
}
}
interface EqualDiagonal{
double getDiagonal();
}

(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
* <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
* component using the component'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 component. 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="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(称为一个编辑单元)里.

用java写个接口实现数据对接

string
clazz="xxx"
area
area=class.forname(clazz).newinstance();
xxx是实现的你接口的类
这就是传说中的父类的引用指向子类的对象java接口对接开发实例,多态
第二句话相当于
area
area=new
xxx();
父类的引用只能调用子类实现的方法java接口对接开发实例,而无法调用子类中存在而父类中不存在的方法
父类的引用指向子类的对象时java接口对接开发实例,其调用方法是用子类的方法去实现的

用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对外接口开发实例的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。 java接口对接开发实例的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java对外接口开发实例、java接口对接开发实例的信息别忘了在本站进行查找喔。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:webpack+vue中使用别名路径引用静态图片地址
下一篇:专属于程序员的浪漫
相关文章

 发表评论

暂时没有评论,来抢沙发吧~