Java 在构造函数参数中创建匿名类传递并实现其接口

Java 在构造函数参数中创建匿名类传递并实现其接口,java,Java,你好, 使用构造函数参数初始化,该构造函数参数具有将在客户端中实现的接口 java version "1.7.0_45" 在我的客户机中,我想在构造函数参数中创建一个匿名类传递并实现接口 然而,这不适用于争论 public class SInvitationListenerImp { public interface MUCRoomListener { void onInvitationReceived(String roomName, String inviter,

你好,

使用构造函数参数初始化,该构造函数参数具有将在客户端中实现的接口

java version "1.7.0_45"
在我的客户机中,我想在构造函数参数中创建一个匿名类传递并实现接口

然而,这不适用于争论

public class SInvitationListenerImp {
    public interface MUCRoomListener {
        void onInvitationReceived(String roomName, String inviter, String reason, String password, String message);
    }

    public SInvitationListenerImp(MUCRoomListener roomListenerEvent, int clientConnection) {
        mMUCRoomListener = roomListenerEvent;
        mClientConnection = clientConnection;
    }
}
这在没有构造函数参数的情况下工作。但是,这些参数是必需的(因此这不适合我的需要)

这是否可以将参数传递给构造函数并实现接口

非常感谢你的建议

应在
'或'''中更新结果

new SInvitationListenerImp.MUCRoomListener() {
      @Override
          public void onInvitationReceived(String s, String s1, String s2, String s3, String s4) {
      }
};

你的语法错了。。。你可能需要这样的东西

new SInvitationListenerImp(new SInvitationListenerImp.MUCRoomListener() {
        @Override
        public void onInvitationReceived(String s, String s1, String s2, String s3, String s4) {
        }
    } MainActivity.this, connection);

谢谢,但那没用。我在预期的最后一个}大括号“.”或“')处出错。我已经用您当前的解决方案更新了我的问题。@ant2009,我刚刚看到您的更新。。。为什么要使用
MainActivity。它是传递给库的类对象,以便它可以通过接口回调事件。这不是问题,因为我已经用int值测试了相同的代码。谢谢。你的构造函数不接受任何这样的对象。。。它只接受两个参数,一个
mucroomstener
和一个
int
。。。你不能通过它的任何其他内容。。。
new SInvitationListenerImp(new SInvitationListenerImp.MUCRoomListener() {
        @Override
        public void onInvitationReceived(String s, String s1, String s2, String s3, String s4) {
        }
    } MainActivity.this, connection);
new SInvitationListenerImp(new SInvitationListenerImp.MUCRoomListener() {
            @Override
            public void onInvitationReceived(String s, String s1, String s2, String s3, String s4) {
                // Interface method body.
            }

        }, connection);