java中泛型类型的组合(composition of generic types in java)
假设我正在定义一个类User,它具有扩展Principal的泛型类型
public abstract class User<P extends Principal> { }
我有一个动作基类,应该与用户参数化:
public abstract class Action<U extends User<Principal>> { }
这两个类都在muy代码库上。 现在,我想在特定的应用程序上使用它们,所以我为Principal和User创建了一个实现:
public class MyPrincipal implements Principal { public String getName() { return "Administrator"; } } public class MyUser extends User<MyPrincipal>{ }
一旦我拥有了我的用户,我就想在一个动作上使用它
public class MyAction extends Action<MyUser>{ }
最后一个类不编译。 它抱怨说:
Bound mismatch: The type MyUser is not a valid substitute for the bounded parameter <U extends User<Principal>> of the type Action<U>
我想要的是强制所有扩展MyAction的类都应该使用扩展User的对象进行参数化,并且还要通过实现Principal的对象进行参数化。 我错过了什么?
Let's say I'm defining a class User, which has a generic type which extends Principal
public abstract class User<P extends Principal> { }
And I have an action base class which should be parameterized with an user:
public abstract class Action<U extends User<Principal>> { }
Those two classes are on muy codebase. Now, I want to use them on an specific application, so I create an implementation for Principal and User:
public class MyPrincipal implements Principal { public String getName() { return "Administrator"; } } public class MyUser extends User<MyPrincipal>{ }
Once I have my user, I want to use it on an action
public class MyAction extends Action<MyUser>{ }
This last class does not compile. It complais that :
Bound mismatch: The type MyUser is not a valid substitute for the bounded parameter <U extends User<Principal>> of the type Action<U>
What I want is to enforce that all classes extending MyAction should be parameterized with an object that extends User, and also is parameterized by an object which implements Principal. What I'm missing?
原文:
最满意答案
您正在客户端引用一个非常旧的版本的socket.io(1.0.0)。 您不指定您在服务器上使用的是哪个版本的socket.io,但理想情况下客户端和服务器应匹配。
socket.io将在服务器的
/socket.io/socket.io.js
路径上为其匹配的客户端脚本提供服务,或者您可以使用来自CDN的更新版本。https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.4/socket.io.js https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket .io.js
你的代码按照预期运行,包括1.x分支的最新版本(1.7.4)和2.x分支的最新版本(2.0.4)
You are referencing a very old version of socket.io (1.0.0) in your client. You don't specify which version of socket.io you're using on the server, but ideally the client and server should match.
socket.io will serve its matching client script at the
/socket.io/socket.io.js
path of your server, or you can use a newer version from a CDN.https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.4/socket.io.js https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js
Your code runs as expected for me with both the latest version from the 1.x branch (1.7.4) and the latest version from the 2.x branch (2.0.4)
相关问答
更多-
我已经解决了我的问题。 这是我改变的: var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = require('socket.io')(server); var fs = require('fs'); app.use(express.static(__dirname + '/public')); app.get('/galerie', ...
-
你好,你只需要通过参数传递IO实例到你的外部模块: app.js let http = require('http').Server(app); let io = require('socket.io')(http); let actividad = require('routesActividad')(io); routesActividad.js: function http(io){ //put the IO stuff wherever you want inside functions ...
-
我在GitHub上有一个端到端的Express和Socket.io工作示例, 网址是https://github.com/hectorcorrea/intro-to-nodejs 尝试这个: git clone https://github.com/hectorcorrea/intro-to-nodejs.git cd intro-to-nodejs npm install npm install socket.io node socketioDemo 然后打开两个浏览器窗口并浏览到http://loca ...
-
配置'最简单'的node.js + socket.IO + Express服务器(Configuring 'simplest' node.js + socket.IO + Express server)[2022-09-22]
删除第一个app.configure包装器,但保留其内容。 一般来说它没用,但特别是如果你没有传递参数。 因为您没有使用它们,请删除methodOverride和bodyParser Thanks for all the replies. Finally have something that works and am posting so someone else may benefit. My first attempt(above) was obviously NOT the simplest sol ... -
用于浏览器客户端和node.js客户端的express,socket.io服务器(express, socket.io server for browser client and node.js client)[2021-04-24]
问题在于我需要依赖的方式。 它适用于其他版本,但在我目前的版本上没有。 node -v v0.12.4 "express": "^4.13.1", "socket.io": "^1.3.6", "socket.io-client": "^1.3.6" 使用这些版本,以下代码有效: 服务器 var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(serve ... -
我正在回答我自己的问题。 我终于找到了解决这个问题的方法,但我仍然不知道原因是什么。 解决方案是在任何其他标记之前移动 (有16个脚本声明:angular,bootstrap,jquery,moment等)。 因此,如果您在浏览器上获得“未捕获的ReferenceError:io未定义”消息,请在页面开头的标签内首先移动“” 。 它将为您节省大量努力以使其工作。 I'm answering my own quest ...
-
node.js / Express / Connect / Socket.io的关系(Relationship of node.js / Express / Connect / Socket.io)[2022-03-26]
Express(或Connect)是HTTP Web应用程序的应用程序框架。 这是您的应用程序的切入点。 它提供了一些非常常见的功能,例如: HTTP服务器 URL路由 请求args 会议 它还允许轻松使用其他功能(称为中间件),例如身份验证处理,模板化。 如果您只想通过SocketIO实现发布/订阅服务,而不使用任何页面或其他API,只需使用Socket.io库(S.io主页示例): var io = require('socket.io').listen(80); io.sockets.on('con ... -
好吧,事实证明这是socket.io的node.js代码中的命名空间的问题。 更多信息: http : //socket.io/docs/rooms-and-namespaces 这是服务器的一个工作示例 var app = require( 'express' )(); var http = require( 'http' ).Server( app ); var io = require( 'socket.io' )( http ); var nsp = io.of('/chat'); // this ...
-
Node.js /socket.io/socket.io.js没有找到express 4.0(Node.js /socket.io/socket.io.js not found express 4.0)[2023-11-24]
你有express明确的路线,可以捕捉所有其他路线吗? 也许这样的东西: app.get("/", handlers.home); app.get("/..." ...); ... app.get("*", handlers.error); 这可能会让socket.io无法为客户端托管它自己的js文件。 有一个简单的方法来解决这个问题,因为你可能已经有一个public或static文件夹设置为快速。 就像是: app.use(express.static( “公共”)); 创建一个名为socket.io的 ... -
socket.io不能在node.js服务器上运行(使用express)(socket.io not work on node.js server (with express))[2024-03-13]
您正在客户端引用一个非常旧的版本的socket.io(1.0.0)。 您不指定您在服务器上使用的是哪个版本的socket.io,但理想情况下客户端和服务器应匹配。 socket.io将在服务器的/socket.io/socket.io.js路径上为其匹配的客户端脚本提供服务,或者您可以使用来自CDN的更新版本。 https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.4/socket.io.js https://cdnjs.cloudflare.com/aj ...