有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在安卓示例蓝牙聊天应用程序中,这个tmp变量的用途是什么

我正在分析安卓的一个示例应用程序——蓝牙聊天:https://developer.安卓.com/samples/BluetoothChat/project.html。我正在研究BluetoothChatService类(https://developer.安卓.com/samples/BluetoothChat/src/com.example.安卓.bluetoothchat/BluetoothChatService.html),它的内部类ConnectThread的构造函数。这里有这样一段代码:

private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    (...)
    public ConnectThread(BluetoothDevice device, boolean secure) {
        (...)
        BluetoothSocket tmp = null;
        (...)
        try {
            if (secure) {
                tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
            } else {
                tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);
            }
        } catch (IOException e) {
            Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
        }
        mmSocket = tmp;
    }
    (...)

我不明白——为什么他们首先将对象分配给tmp值,然后将其复制到mmSocket属性?他们可以这样做,简单一点:

private class ConnectThread extends Thread {
    private final BluetoothSocket mmSocket;
    (...)
    public ConnectThread(BluetoothDevice device, boolean secure) {
        (...)
        try {
            if (secure) {
                mmSocket =  device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
            } else {
                mmSocket =  device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);
            }
        } catch (IOException e) {
            Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
        }
    }

共 (0) 个答案