简体   繁体   English

您如何传递对“类”类型的引用?

[英]How do you pass a reference to type 'class'?

I have a method that registers classes for serialization. 我有一个注册类以进行序列化的方法。 I want to call this from a control class, something like: 我想从控件类中调用它,例如:

    public static void main(String[] args) {
        Registrar.RegisterClass(Control.SomeRequest);
        Registrar.RegisterClass(Control.SomeResponse);

        Sender testServer = new Sender();
        Receiver testClient = new Receiver();

        testServer.StartServer();
        testClient.StartClient();
    }

    public static class SomeRequest {
        public String text;
    }
    public static class SomeResponse {
        public String text;
    }
---------------------------------------------
public class Registrar {
    static Kryo kryo = client.getKryo();

    public static void RegisterClass(??? cls){
        kryo.register(cls.class);
    }
}

Alternatively, I could pass in Control.SomeRequest.class just as easy, though I'm not sure how to achieve either of these. 另外,我也可以轻松传递Control.SomeRequest.class ,尽管我不确定如何实现这两个目标。

The class name of "classes" is Class “ classes”的类名称是Class

public static void RegisterClass(Class cls){
        kryo.register(cls);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将泛型类型('Object myVar')作为参数传递来替换 class 类型('SomeClass.class')? - How do you pass a generic type ('Object myVar') as a parameter to replace a class type ('SomeClass.class')? 如果类具有引用类型成员java,如何实现equality和hashCode方法? - how do you implement the equality and hashCode method if the class has reference type members java? 如何从另一个 class 传递 class 参考? - How do I pass a class reference from another class? 如何将对外部类的引用传递给内部类中的方法? (或者如何将“ this”传递给内部类?) - How do I pass a reference to the outer class to a method in an inner class? ( Or how do I pass “this” to an inner class? ) 如何将类的实例作为该类方法的参数传递? - How do you pass an instance of a class as an argument for a method of that class? 当您需要在Java中通过引用传递为多个参数赋值时,您是如何做到的? - When you need pass by reference in Java to assign values to multiple parameters, how do you do it? 如何通过引用传递原始数据类型? - How do I pass a primitive data type by reference? 如何在加载课程时告诉参考路径? - How do you tell the reference path when loading a class? 在UML类图中,如何指定类的实际类型? - In a UML Class diagram, how do you specify Actual type of a class? 如何在Java中将变量从一类传递到另一类? - How Do You Pass a Variable from One Class to Another in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM