088-Java复习 (一)

 

 

多线程

public class Test03 {

    public static void main(String[] args) {
        new MyThread().start();
        new Thread(new MyRunnable()).start();
    }

}

class MyThread extends Thread {
    @Override
    public void run() {
        super.run();
        System.out.println("MyThread run");
    }
}

class MyRunnable implements Runnable {

    @Override
    public void run() {
        System.out.println("MyRunnable run");
    }
}

 

 

 

 

设计模式

1.Adapter Pattern 适配器模式
举例
现在获得用户名字的方法是
Person person = backend.getPerson(name);
Name name=person.getName();

但是如果Person类被替换成了Customer类
那么就要用到适配器模式
Customer c= backend.getCustomer(name);
Adapter a= new Adapter(c);
Person person= a.getPerson();
Name name= person.getName();

 

 

 

 

2. Singleton Pattern 单例模式
一个类的对象只有一个,就是单例模式
例如

public class Single{
    private Single();
    private static Single instance=new Single();
    public static Single instance(){ return instance;}
}

 

 

 

 

 

3. Iterator Pattern 迭代器模式
迭代器模式就是模仿hasNext进行迭代
没什么好说的
也没什么用

 

 

 

 

4. Command Pattern 命令模式
给一个命令,执行一个方法
没什么好说的

 

 

 

 

 

 

UDP编程

写一个Server

package test02;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UDPServerTest extends Application {

    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root);
        display = new TextArea();
        display.setPrefColumnCount(20);
        display.setPrefRowCount(10);
        display.setEditable(false);
        ScrollPane scrollDisplay = new ScrollPane(display);
        root.getChildren().add(scrollDisplay);
        new UDPServerThread(this).start();
        primaryStage.setTitle("UDP Server Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    public void AppendDisplay(String msg) {
        display.appendText(msg);
    }

    private TextArea display;
}

class UDPServerThread extends Thread {
    public UDPServerThread(UDPServerTest app) {
        mainApp = app;
    }

    public void run() {
        try {
            // construct a socket on port 5000
            socket = new DatagramSocket(5000);
        } catch (SocketException se) {
            se.printStackTrace();
            System.exit(1);
        }
        while (true) {
            try {
                // Set up a buffer packet
                byte data[] = new byte[100];
                receivePacket = new DatagramPacket(data, data.length);// Wait for packet
                socket.receive(receivePacket);// Process packet
                sendPacket = new DatagramPacket(receivePacket.getData(), receivePacket.getLength(), receivePacket.getAddress(), receivePacket.getPort());// Echo information back to client
                socket.send(sendPacket);
                String info = "\nPacketreceived from host: " + receivePacket.getAddress() + "\nHostport: " + receivePacket.getPort() + "\nLength: " + receivePacket.getLength() + "\nContents: " + new String(receivePacket.getData(), 0, receivePacket.getLength());
                Platform.runLater(new Runnable() {
                    public void run() {
                        mainApp.AppendDisplay(info);
                        mainApp.AppendDisplay("\n\nEcho data to client\n");
                        mainApp.AppendDisplay("Packet sent\n");
                    }
                });
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

    private UDPServerTest mainApp;
    private DatagramPacket sendPacket, receivePacket;
    private DatagramSocket socket;
}

 

 

再写一个Client

package test02;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.*;

public class UDPClientTest extends Application {

    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root);
        VBox vbox = new VBox(5);
        // spacing between child nodes only.
        vbox.setPadding(new Insets(10));
        // space between vbox border and child nodes column
        enter = new TextField();
        enter.setPrefColumnCount(20);
        display = new TextArea();
        display.setPrefColumnCount(20);
        display.setPrefRowCount(10);
        display.setEditable(false);
        ScrollPane scrollDisplay = new ScrollPane(display);
        vbox.getChildren().addAll(enter, scrollDisplay);
        root.getChildren().add(vbox);
        UDPClient = new UDPClientThread(this);
        UDPClient.start();
        enter.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent e) {
                AppendDisplay("\n\nSending packet containing: " + enter.getText() + "\n");
                UDPClient.SendPacket(enter.getText());
                AppendDisplay("Packet sent\n");
                enter.setText("");
            }
        });
        primaryStage.setTitle("UDP Client Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    public void AppendDisplay(String msg) {
        display.appendText(msg);
    }

    UDPClientThread UDPClient;
    private TextField enter;
    private TextArea display;
}

class UDPClientThread extends Thread {
    public UDPClientThread(UDPClientTest app) {
        MainApp = app;
    }

    public void SendPacket(String msg) {
        try {
            byte data[] = msg.getBytes();
            // destination: local machine, port: 5000
            sendPacket = new DatagramPacket(data, data.length,
                    InetAddress.getLocalHost(), 5000);
            socket.send(sendPacket);
        } catch (UnknownHostException uhe) {
            uhe.printStackTrace();
            System.exit(3);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    public void ReceivePacket() {
        try {
            // Set up packet as buffer
            byte data[] = new byte[100];
            receivePacket = new DatagramPacket(data, data.length);
            // Wait for packet
            socket.receive(receivePacket);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    private UDPClientTest MainApp;
    private DatagramPacket sendPacket, receivePacket;
    private DatagramSocket socket;

    public void run() {
        try {
            // Create a socket
            socket = new DatagramSocket();
            while (true) {
                ReceivePacket();
                // info in receivePacket object
                String info = "\nPacket received" + "\nFrom host: "
                        + receivePacket.getAddress() + "\nHost port: "
                        + receivePacket.getPort() + "\nlength: "
                        + receivePacket.getLength() + "\nContaining: "
                        + new String(receivePacket.getData(),
                        0, receivePacket.getLength());
                Platform.runLater(new Runnable() {
                    public void run() {
                        MainApp.AppendDisplay(info);
                    }
                });
            }
        } catch (SocketException se) {
            se.printStackTrace();
            System.exit(1);
        }
    }
}

 

 

 

 

 

 

 

command design pattern 命令模式

public class Test01 {
    public static void main(String[] args) {
        OperateList list = new OperateList();
        list.add(new Num(1, 2));
        list.add(new Num(43, 26));
        list.add(new Num(65, 75));

        list.map(new CommandAdd() {
            @Override
            public void execute(Object data) {
                if (data instanceof Num) {
                    Num num = (Num) data;
                    System.out.println(num.a + num.b);
                }
            }
        });
    }
}

class OperateList extends ArrayList {
    public void map(CommandAdd c) {
        for (Object data : this) {
            c.execute(data);
        }
    }
}

class Num {
    int a;
    int b;

    Num(int a, int b) {
        this.a = a;
        this.b = b;
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值