java和adapter的简单介绍

博主:adminadmin 2023-01-28 03:30:09 270

今天给各位分享java和adapter的知识,其中也会对进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Java中,为什么需要Adapter类

将两个不兼容的类纠合在一起使用,属于结构型模式,需要有Adaptee(被适配者)和Adaptor(适配器)两个身份.

为何使用?

我们经常碰到要将两个没有关系的类组合在一起使用,第一解决方案是:修改各自类的接口,但是如果我们没有源代码,或者,我们不愿意为了一个应用而修改各自的接口。

怎么办?

使用Adapter,在这两种接口之间创建一个混合接口(混血儿).

java里面的适配器是什么东西

适配器,我们知道充电器有适配器。可以把220V的电压,转换成110V的电压。

Java语言中的适配器,就是可以把一种接口转换成另一种接口的对象。

//如一个类

interface Door {

    void open();

    void close();

}

//而我们需要另一种类

interface MyDoor {

    void setOpen(boolean open);

}

//那我们就可以写一个适配器

class MyDoorAdapter implements MyDoor {

    private Door door;

    public MyDoorAdapter(Door door) {

        this.door = door;

    }

    public void setOpen(boolean open) {

        if (open) {

            door.open();

        } else {

            door.close();

        }

    }

}

//Test

public class Test {

    public static void main(String[] args) {

        Door door = ...//假设我们已经有一个door对象了

        MyDoor myDoor = new MyDoorAdapter(door);

        myDoor.setOpen(true);//看我们已经有了一个MyDoor对象

        //适配器主要用于动态修饰对象,即有现成对象的情况下,对其进行增强或转变

    }

}

java网络编程应该怎样在客户端和服务器间实现通信?

以前写的,照贴了。。。服务器端:import java.awt.*;\x0d\x0aimport java.awt.event.WindowAdapter;\x0d\x0aimport java.awt.event.WindowEvent;\x0d\x0aimport java.io.*;\x0d\x0aimport java.net.*;/*6、 采用UDP协议,编写一个Java网络应用程序,该应用分服务器端程序和客户端程序两部分。\x0d\x0a* 客户端指定一个服务器上的文件名,让服务器发回该文件的内容,或者提示文件不存在。\x0d\x0a* (20分)(服务端程序和客户端程序分别命名为Server.java和Client.java)*/\x0d\x0apublic class N4BT6 extends Frame\x0d\x0a{\x0d\x0aDatagramSocket socket ;\x0d\x0aDatagramPacket packet ;byte[] buf ;\x0d\x0aFile file ;\x0d\x0aFileInputStream input;\x0d\x0aString message = "该文件不存在";\x0d\x0aTextArea text;\x0d\x0apublic N4BT6(String title)\x0d\x0a{\x0d\x0asuper(title);\x0d\x0atext = new TextArea(6,4);\x0d\x0aadd(text);\x0d\x0asetSize(400, 300);\x0d\x0asetVisible(true);\x0d\x0aaddWindowListener(new WindowAdapter()\x0d\x0a{\x0d\x0apublic void windowClosing(WindowEvent e)\x0d\x0a{\x0d\x0adispose();\x0d\x0a}\x0d\x0a});\x0d\x0a\x0d\x0abuf = new byte[1024];\x0d\x0atry\x0d\x0a{\x0d\x0asocket = new DatagramSocket(1230);\x0d\x0apacket = new DatagramPacket(buf, buf.length);\x0d\x0asocket.receive(packet);\x0d\x0afile = new File(new String(packet.getData()));\x0d\x0asocket = new DatagramSocket();\x0d\x0a} \x0d\x0acatch (Exception e)\x0d\x0a{e.printStackTrace();\x0d\x0a}\x0d\x0a\x0d\x0aif(file.exists())\x0d\x0a{\x0d\x0atry\x0d\x0a{\x0d\x0abuf = new byte[(int)file.length()];\x0d\x0apacket = new DatagramPacket(buf,buf.length,InetAddress.getLocalHost(),1234);\x0d\x0ainput = new FileInputStream(file);\x0d\x0ainput.read(buf);\x0d\x0asocket.send(packet);\x0d\x0a}\x0d\x0acatch (IOException e) \x0d\x0a{\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0aelse\x0d\x0a{\x0d\x0atry\x0d\x0a{\x0d\x0apacket = new DatagramPacket(message.getBytes(),message.getBytes().length,\x0d\x0aInetAddress.getLocalHost(),1234);\x0d\x0asocket.send(packet);\x0d\x0a}\x0d\x0acatch (Exception e) \x0d\x0a{\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a}\x0d\x0apublic static void main(String[] args)\x0d\x0a{\x0d\x0anew N4BT6("Server");\x0d\x0a}\x0d\x0a}\x0d\x0a客户端:import java.awt.*;\x0d\x0aimport java.awt.event.*;\x0d\x0aimport java.net.DatagramPacket;\x0d\x0aimport java.net.DatagramSocket;\x0d\x0aimport java.net.InetAddress;public class N4BT6_2 extends Frame\x0d\x0a{\x0d\x0aTextArea text;\x0d\x0aString message = "Q.txt";\x0d\x0aDatagramSocket socket ;\x0d\x0aDatagramPacket packet;\x0d\x0abyte[] buf;\x0d\x0apublic N4BT6_2(String title)\x0d\x0a{\x0d\x0asuper(title);\x0d\x0atext = new TextArea(6,4);\x0d\x0aadd(text);\x0d\x0asetSize(400, 300);\x0d\x0asetVisible(true);\x0d\x0aaddWindowListener(new WindowAdapter()\x0d\x0a{\x0d\x0apublic void windowClosing(WindowEvent e)\x0d\x0a{\x0d\x0adispose();\x0d\x0a}\x0d\x0a});\x0d\x0atry\x0d\x0a{\x0d\x0a\x0d\x0asocket = new DatagramSocket();\x0d\x0apacket = new DatagramPacket(message.getBytes(),message.getBytes().length,\x0d\x0aInetAddress.getLocalHost(),1230);\x0d\x0asocket.send(packet);\x0d\x0a}\x0d\x0acatch (Exception e) \x0d\x0a{\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a\x0d\x0atry\x0d\x0a{\x0d\x0abuf = new byte[1024];\x0d\x0asocket = new DatagramSocket(1234);\x0d\x0apacket = new DatagramPacket(buf,buf.length);\x0d\x0asocket.receive(packet);\x0d\x0atext.append(new String(buf));\x0d\x0a}\x0d\x0acatch (Exception e) \x0d\x0a{\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0apublic static void main(String[] args)\x0d\x0a{\x0d\x0anew N4BT6_2("Client");\x0d\x0a}\x0d\x0a}

java中接口和适配器的概念

其实适配器只是一个类,它实现了某种接口,提供了方法体。

这样,再用到这个接口时,可以直接继承适配器,这样就不需要把接口中的每一个方法再填充一遍了,只需要在这个类中复写一下需要用的方法。这样简单,方便。

java中Adapter是什么概念

Adapter乃适配器,适配不协调之两者,看维基一解释:

A device or application used to achieve operative compatibility between devices that otherwise are incompatible.

He had an adapter that let him plug his phone into the car's cigarette lighter for power.

说的是手机和车载电源插座之间的适配器连接了本身不协调的两者,这就是适配器adapter的功能。

JDK DOC中对KeyAdapter的解释:

public abstract class KeyAdapter

extends Object

implements KeyListener

An abstract adapter class for receiving keyboard events. The methods in this class are empty. This class exists as convenience for creating listener objects.

Extend this class to create a KeyEvent listener and override the methods for the events of interest. (If you implement the KeyListener interface, you have to define all of the methods in it. This abstract class defines null methods for them all, so you can only have to define methods for events you care about.)

Create a listener object using the extended class and then register it with a component using the component'saddKeyListener method. When a key is pressed, released, or typed, the relevant method in the listener object is invoked, and the KeyEvent is passed to it.

这里它适配了什么呢?原本如果没有KeyAdapter,那就用实现了KeyListener接口的类来作listener,这样就不得不实现许多不需要实现的方法,逻辑上是不适配的,所以KeyAdapter应运而生,适配!

JAVA适配器是一个接口吗

适配器模式基本介绍:

1) 适配器模式(Adapter Pattern)将某个类的接口转换成客户端期望的另一个接口表示,主的目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同工作。其别名为包装器(Wrapper)

2) 适配器模式属于结构型模式

3) 主要分为三类:类适配器模式、对象适配器模式、接口适配器模式

例如,有个A类(里面有个m2方法),有个B类(里面有个m1方法),m2想要调用m1的方法,但是由于一些原因,不能直接调用(有可能参数不合适,或者别的原因,但是方法大同小异),这样可以写一个Adapter类,直接让A类的m2调用adapter就可以了,让Adapter类适配A类和B类,A类通过适配器调用B,B通过适配器调用A.

适配器模式工作原理:

1) 适配器模式:将一个类的接口转换成另一种接口.让原本接口不兼容的类可以兼容

2) 从用户的角度看不到被适配者,是解耦的

3) 用户调用适配器转化出来的目标接口方法,适配器再调用被适配者的相关接口方法

4) 用户收到反馈结果,感觉只是和目标接口交互,如图 

一。类适配器模式:

1.类适配器模式介绍

基本介绍:Adapter 类,通过继承 src 类,实现 dst  类接口,完成 src-dst 的适配。src是要使用适配器的类,dst是目标,src类通过适配器调用dst

关于java和adapter和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。