Java – 如何调用泛型实例方法

huangapple go评论74阅读模式
英文:

Java - how to call a generic instance method

问题

class Connection
{
    private Socket m_socket;
    private ObjectOutputStream m_send;
    private ObjectInputStream m_recv;

    // Methods for initializing socket omitted

    public <Type extends Serializable> Type readObject() throws IOException, ClassNotFoundException
    {
        return (Type)m_recv.readObject(); 
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Connection conn = new Connection();

        // 无法正常工作。
        // 错误:-> 期望
        ServerResponse resp = conn.readObject<ServerResponse>();
    }
}
为什么我无法使用提供的类型调用readObject而只能使用conn.readObject()调用它
英文:
class Connection
{
    private Socket m_socket;
    private ObjectOutputStream m_send;
    private ObjectInputStream m_recv;

    // Methods for initializing socket omitted

    public &lt;Type extends Serializable&gt; Type readObject() throws IOException, ClassNotFoundException
    {
        return (Type)m_recv.readObject(); 
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Connection conn = new Connection();

        // Doesn&#39;t work.
        // error: -&gt; expected
        ServerResponse resp = conn.readObject&lt;ServerResponse&gt;();
    }
}

Why I am not able to call readObject with supplied type but instead can call it using only conn.readObject().

答案1

得分: 0

请查看!这对我有效。

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

public class Connection {

    private Socket m_socket;
    private ObjectOutputStream m_send;
    private ObjectInputStream m_recv;

    // 初始化套接字的方法被省略

    public <Type extends Serializable> Type readObject() throws IOException, ClassNotFoundException {
        return (Type)m_recv.readObject();
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Connection conn = new Connection();
        ServerResponse resp = conn.readObject();
        // 这是完整的语法
        // ServerResponse resp = conn.<ServerResponse>readObject();
    }
}

class ServerResponse implements Serializable {}
英文:

Check this out! It works to me.

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

public class Connection {

    private Socket m_socket;
    private ObjectOutputStream m_send;
    private ObjectInputStream m_recv;

    // Methods for initializing socket omitted

    public &lt;Type extends Serializable&gt; Type readObject() throws IOException, ClassNotFoundException {
        return (Type)m_recv.readObject();
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Connection conn = new Connection();
        ServerResponse resp = conn.readObject();
        // This is a full syntax
        // ServerResponse resp = conn.&lt;ServerResponse&gt;readObject();
    }
}

class ServerResponse implements Serializable {}

huangapple
  • 本文由 发表于 2020年8月21日 03:07:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63511699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定