这是一个简单的代码
class Foo {
}
class Bar extends Foo {
}
public class Main {
public static void main(String[] args) throws Exception {
fn(null);
}
static void fn(Foo f) {
System.out.println(f instanceof Foo ? "Foo" : "Bar");
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:Java如何知道传递的null是Bar不是Foo?我知道为什么编译器选择Bar而不是Foo(因为有从foo到bar和bar到foo的转换,反之亦然).但是这个方法怎么会知道这个null来自Bar而不是Foo?null是否包含有关分配给的对象的一些信息?
假设我有这三个类:
class Foo {
void fn() {
System.out.println("fn in Foo");
}
}
class Mid extends Foo {
void fn() {
System.out.println("fn in Mid");
}
}
class Bar extends Mid {
void fn() {
System.out.println("fn in Bar");
}
void gn() {
Foo f = (Foo) this;
f.fn();
}
}
public class Trial {
public static void main(String[] args) throws Exception {
Bar b = new Bar();
b.gn();
}
}
Run Code Online (Sandbox Code Playgroud)
是否有可能调用Foo的fn()?我知道我的解决方案gn()不起作用,因为this它指向一个类型的对象Bar.
我知道java中的数组扩展了对象,所以为什么将它们作为params传递不起作用.
public static void main(String[] args) {
foo(new Integer[]{1, 2, 3}); // 1
foo(new int[]{1,2,3}); //2
}
static void foo(Object... params) {
System.out.println(params[0]);
}
Run Code Online (Sandbox Code Playgroud)
此外,为什么它不将数组视为单个参数(第1行)
运行以上输出是:
1
[I@3e25a5
Run Code Online (Sandbox Code Playgroud) 我正在Linux(Ubuntu)下尝试Mono/.Net 3.5.我尝试在Mono中使用线程,但它似乎无法正常工作.
public static void Main (string[] args)
{
Thread thread =new Thread(()=> fn("first"));
Thread thread1=new Thread(()=> fn("second"));
thread.Start();
thread1.Start();
}
static void fn(string name)
{
for(int i=0;i<10;i++)
Console.WriteLine(i+" "+name);
}
Run Code Online (Sandbox Code Playgroud)
两个循环都按顺序运行,就好像我没有使用线程一样.
怎么了?
我已禁用行计数,SET NOCOUNT ON但似乎sqlserver仍在计算行数.
USE Northwind
SET NOCOUNT ON;
SELECT * FROM Products p
SELECT @@ROWCOUNT AS 'RowCount';
Run Code Online (Sandbox Code Playgroud)
对于行计数,查询返回77,为什么?
我有一个Servlet与static Mongo = new Mongo()和Morphia morphia = new Morphia()对象.每次GET调用时,我都会执行以下操作:
doGet(...){
...
datastore = morphia.createDatastore(mongo, dbName);
...
}
Run Code Online (Sandbox Code Playgroud)
我没有关闭datastore,因为没有密切的方法.每次调用servlet时,mongo中使用的连接数增加:
{ "current" : 113, "available" : 706, "totalCreated" : NumberLong(122) }
> db.serverStatus().connections { "current" : 115, "available" : 704, "totalCreated" : NumberLong(124) }
> db.serverStatus().connections { "current" : 116, "available" : 703, "totalCreated" : NumberLong(125) }
> db.serverStatus().connections { "current" : 121, "available" : 698, "totalCreated" : NumberLong(130) }
> db.serverStatus().connections …Run Code Online (Sandbox Code Playgroud) 此代码在运行时失败,而不是在编译时失败.我不是C++专家.有帮助吗?
extern void fn();
int main(int argc, char** argv) {
fn();
}
void fn()
{
struct Foo{
string name;
}*foo;
foo->name="sleiman";
cout<<foo->name<<endl;
}
Run Code Online (Sandbox Code Playgroud)
为什么这段代码在运行时失败,为什么在创建静态实例时它会成功?
void fn()
{
struct Foo{
string name;
}foo;
foo.name="sleiman";
cout<<foo.name<<endl;
}
Run Code Online (Sandbox Code Playgroud)