gpt4 book ai didi

java - 将 String 转换为泛型类型 >

转载 作者:行者123 更新时间:2023-12-01 16:13:46 25 4
gpt4 key购买 nike

我正在编写一个 GUI 客户端来操作和显示二叉树。这里棘手的部分是树是通用类型。我所处的情况是,我必须在 Message<T> 的帮助下向我的服务器传递从用户获得的值。 ,它是一个可序列化对象,存储服务器的请求及其值。我的问题是类型 T 的值使用 JOptionPane.showInputDialog 获得,返回 String

有没有办法以 T 的形式获取用户的输入输入 或 来转换 String到提到的类型?

主类:

import javax.swing.JOptionPane;

public class Main {

public static void main(String[] args) {

Object[] options = {"Integer", "Double", "String"};
int result = JOptionPane
.showOptionDialog(null, "Please, select the type of the tree:", "Selection of the type",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, null);

switch (result) {
case 0:
MyFrame<Integer> frameInteger = new MyFrame<>("Integer");
frameInteger.setVisible(true);
break;
case 1:
MyFrame<Double> frameDouble = new MyFrame<>("Double");
frameDouble.setVisible(true);
break;
case 2:
MyFrame<String> frameString = new MyFrame<>("String");
frameString.setVisible(true);
default:
throw new IllegalArgumentException("Invalid type.");
}
}

}

JFrame/客户端类:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class MyFrame<T extends Comparable<T>> extends JFrame {

Socket client;
JPanel context = new JPanel();
String address;
ObjectInputStream in;
ObjectOutputStream out;
BinaryTree<T> tree;
boolean result;
String type;

public MyFrame(String type) {
this.type = type;
address = JOptionPane.showInputDialog("Please enter the address and port (e.g. 127.0.0.1:5555)");
try {
client = new Socket(address.split(":")[0], Integer.parseInt(address.split(":")[1]));
in = new ObjectInputStream(client.getInputStream());
out = new ObjectOutputStream(client.getOutputStream());
} catch (Exception e) {
System.err.println("It wasn't possible to obtain the address from the given input.");
}

setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1024, 720);
setLocationRelativeTo(null);
setTitle("Binary Tree Client");

initUI();
}

private void initUI() {
JMenuBar menuBar = new JMenuBar();
JMenu file = new JMenu("File...");
JMenu edit = new JMenu("Edit");

JMenuItem close = new JMenuItem("Close");

JMenuItem insert = new JMenuItem("Insert...");
JMenuItem delete = new JMenuItem("Delete...");
JMenuItem search = new JMenuItem("Search...");

close.addActionListener(e -> System.exit(0));

insert.addActionListener(e -> {
String value = JOptionPane.showInputDialog("Please insert the node value:");

// here is the problem
tree = exchange(new ClientMessage<T>("INSERT", value)).getTree();

});

file.add(close);

edit.add(insert);
edit.add(delete);
edit.add(search);

menuBar.add(file);
menuBar.add(edit);

setJMenuBar(menuBar);
}

public ServerMessage<T> exchange(ClientMessage<T> clientMsg) {
try {
out.writeObject(clientMsg);
return (ServerMessage<T>) in.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
}

ClientMessage 类:

import java.io.Serializable;

public class ClientMessage<T extends Comparable<T>> implements Serializable {

private String request;
private T value;

public ClientMessage(String request, T value) {
this.request = request;
this.value = value;
}

public String getRequest() {
return request;
}

public T getValue() {
return value;
}
}

以及我想要使用该值的ServerThread类:

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ServerThread<T extends Comparable<T>> extends Thread {

BinaryTree<T> tree;
ObjectInputStream in;
ObjectOutputStream out;
Socket client;

public ServerThread(Socket client) {
this.client = client;
}

@Override
@SuppressWarnings("unchecked")
public void run() {
try {
in = new ObjectInputStream(client.getInputStream());
out = new ObjectOutputStream(client.getOutputStream());

ClientMessage<T> request;
ServerMessage<T> response = new ServerMessage<>();

while ((request = (ClientMessage<T>) in.readObject()) != null) {
handleRequest(request, response);

response.setTree(tree);
out.writeObject(response);
}

} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}

private void handleRequest(ClientMessage<T> request, ServerMessage<T> response) {

switch (request.getRequest()) {
case "SET":
tree = new BinaryTree<>();
break;
case "INSERT":
tree.insert(request.getValue());
break;
case "DELETE":
tree.delete(request.getValue());
break;
case "SEARCH":
response.setResult(tree.search(request.getValue()));
break;
default:
throw new IllegalArgumentException("Invalid request.");
}
}
}

最佳答案

更改您的 MyFrame类接受Class<T> :

Class<T> type;

public MyFrame(Class<T> type) {
this.type = type;
...
}

并调用如下:

MyFrame<Integer> frameInteger = new MyFrame<>(Integer.class);

或者,因为您显然没有使用 type字段,只需删除 type字段和构造函数参数,并调用如下:

MyFrame<Integer> frameInteger = new MyFrame<>();

关于java - 将 String 转换为泛型类型 <T extends Comparable<T>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62457921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com