我试图在java中创建一个Key Listener但是当我尝试时
KeyListener listener = new KeyListener();
Run Code Online (Sandbox Code Playgroud)
Netbeans告诉我KeyListener是抽象的;无法实例化.我知道我错过了这个关键监听器的其他部分,但由于这是我第一次使用关键监听器,我不确定我还需要什么.为什么告诉我这个?
谢谢,
托梅克
我创建了一个派生自System.Windows.Forms.ContextMenuStrip类的类,而不是用户控件,只是一个带有构造函数和一个事件处理程序的普通.cs类.
当我将这个类从工具箱拖到设计器上时,它会为它创建一个私有成员和几个属性,但不会实例化一个对象.
因此,在运行时我得到"对象引用未设置为对象的实例.",因为设计者从不生成该行:
this.searchGridContextMenu1 = new SearchGridContextMenu();
在InitializeComponent中.
它曾用于生成这一行,事实上,我一直将它从我的Vault存储库中重新插入,但设计师只是"再次"使用它.
更新:我现在尝试使用相同的类创建用户控件,它只是这样做有同样的问题.
我有一个工厂应该创建在运行时从类Foo继承的对象.我认为System.Activator.CreateInstance的返回类型与它创建的对象的类型相同,但从以下错误消息判断,其返回类型是Object.
错误1无法将类型'object'隐式转换为'cs_sandbox.Foo'.存在显式转换(您是否缺少演员表?)F:\ projects\cs_sandbox\Form1.cs 46 24 cs_sandbox
好吧,也许我很缺少强制的,但
return (t)System.Activator.CreateInstance(t);
Run Code Online (Sandbox Code Playgroud)
导致又一条错误信息 - 我必须承认 - 这对我没有意义:
错误1找不到类型或命名空间名称't'(您是否缺少using指令或程序集引用?)F:\ projects\cs_sandbox\Form1.cs 45 25 cs_sandbox
这是我的代码:
class Foo { }
class FooChild1 : Foo { }
class FooChild2 : Foo { }
class MyFactory
{
public static Foo CreateInstance(string s)
{
Type t;
if (s.StartsWith("abcdef"))
{
t = typeof(FooChild1);
return System.Activator.CreateInstance(t);
}
else
{
t = typeof(FooChild2);
return System.Activator.CreateInstance(t);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我该如何修复此代码?或者,如果它不可修复,那么在运行时创建从特定类继承的对象的其他方法是什么?
如果实例化中存在错误,是否有办法停止C++类?比如,返回NULL可能吗?基本上我有一个MySQL的包装类,构造函数进行连接,但如果连接失败,我希望对象是,嗯,没用?
PDB::PDB(string _DB_IP, string _DB_USER, string _DB_PASS, string _DB_DB)
: _DB_IP( _DB_IP ), _DB_USER( _DB_USER ), _DB_PASS( _DB_PASS ), _DB_DB( _DB_DB )
{
mysql_init(&this->mysql);
this->connection = mysql_real_connect(&this->mysql, this->_DB_IP.c_str(), this->_DB_USER.c_str(), this->_DB_PASS.c_str(), this->_DB_DB.c_str(), 0, 0, 0);
if( this->connection == NULL ) // WHAT SHOULD I DO HERE, OTHER THAN THROW AN ERROR?
{
cout << mysql_error(&this->mysql) << endl;
}
this->result = NULL;
}
Run Code Online (Sandbox Code Playgroud)
我应该怎么做NULL测试,停止创建等?
我有一个叫做UserContext跟踪我网站上给定用户活动的课程.它应该是一个单例类(每个用户只有一个实例).在Windows窗体应用程序中,我可以写这样的东西:
Class UserContext
Public Shared Current As New UserContext()
Private Sub New(appName As String)
[...]
End Class
Run Code Online (Sandbox Code Playgroud)
但是在ASP.net应用程序上,这将在所有当前用户之间共享.
如果这个类只在一个Page实体中使用,我可以将UserContext实例存储在一个Page变量中 - 它不一定需要在回发中存活.但是其他实体(不了解Page)也调用UserContext,我希望它们都被赋予相同的实例.
我该怎么做才能确保每个http请求(或每个用户)只对一个类进行一次实例化? 我可以使用缓存吗?
Public Shared Function GetContext() As UserContext
If HttpContext.Current.Cache("CurrentUserContext") Is Nothing Then HttpContext.Current.Cache("CurrentUserContext") = New UserContext()
Return HttpContext.Current.Cache("CurrentUserContext")
End Function
Run Code Online (Sandbox Code Playgroud)
可能会议状态是更好的选择吗?
缓存和会话状态都存在回发 - 是否有另一个选项随每个新请求重置?
谢谢你的帮助!
在以下代码中,抛出TypeError: string is not a function第30行var myColour = new colour(255,255,255);.任何人都可以看到代码有什么问题.非常感谢.
var bodies = [];
var orbits = [];
var colours = [
new colour(45, 45, 45),
new colour(255, 0, 0),
new colour(0, 157, 255),
new colour(77, 250, 81),
new colour(255, 247, 0)
];
function colour(red, green, blue)
{
this.red = red;
this.green = green;
this.blue = blue;
};
window.onload = function() {
var c = document.getElementById("theCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();
for (var colour …Run Code Online (Sandbox Code Playgroud) 我有一个webservice,在初始化时实例化一个带有数据源的QueryRunner.它将这一个QueryRunner对象用于webapp使用的多个不同servlet的所有servlet请求,方法是将其作为servlet上下文属性传递.即:
// in servlet context listener (on app initialization)
QueryRunner myQueryRunner = new QueryRunner(myDataSource);
myServletContext.setAttribute("queryRunner", myQueryRunner);
// in the servlets
QueryRunner myQueryRunner = (QueryRunner) myServletContext.getAttribute("queryRunner");
myQueryRunner.query(myStoredProcedure, handler, params)
Run Code Online (Sandbox Code Playgroud)
我想弄清楚这是不是瓶颈.servlet是否应该为QueryRunner每个请求实例化一个新的?
在寻找答案时,我也发现了这个AsyncQueryRunner.但我更加困惑,因为QueryRunner和AsyncQueryRunner的API文档中的解释说的完全相同.
我查看了这里的示例,似乎应该对每个请求进行实例化,但我不确定这是否只是因为它是示例代码.
换句话说,当QueryRunner我使用DBUtils时:
QueryRunner每个请求都使用一个实例?(我现在在做什么)QueryRunner每个servlet请求都实例化一个新的?AsyncQueryRunner每个请求都使用一个实例?我想知道是否有一种快速方法来初始化python中的对象.
例如,在c#中,您可以实例化一个对象并设置字段/属性,如...
SomeClass myObject = new SomeClass() { variableX = "value", variableY = 120 };
Run Code Online (Sandbox Code Playgroud)
谢谢
布赖恩
我知道自Java 7以来,在实例化过程中在构造函数中重复泛型类的类型是一种冗余.但是钻石操作员<>怎么样,重复它呢?换句话说,我想知道这有什么区别:
List<String> Fruits = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
还有这个
List<String> Fruits = new ArrayList();
Run Code Online (Sandbox Code Playgroud)
或这个
Map<Integer, String> students = new HashMap<>();
Run Code Online (Sandbox Code Playgroud)
还有这个
Map<Integer, String> students = new HashMap();
Run Code Online (Sandbox Code Playgroud)
先感谢您
以下代码无法按预期编译:
#include<iostream>
class Enclosing {
int x;
class Nested { int y; };
void EnclosingFun(Nested *n) {
std::cout << n->y; // Compiler Error: y is private in Nested
}
};
Run Code Online (Sandbox Code Playgroud)
但是,如果我将EnclosingFun更改为模板成员函数,编译器(gcc-7)不会抱怨访问y:
#include<iostream>
class Enclosing {
int x;
class Nested { int y; };
template <typename T>
void EnclosingFun(Nested *n, T t) {
std::cout << t << n->y; // OK? Why?
}
};
Run Code Online (Sandbox Code Playgroud)
这是gcc中的错误吗?或者c ++对模板成员函数有不同的访问规则来访问嵌套类?