我正在使用以下类型类:
module T where
class T a where
v :: a
Run Code Online (Sandbox Code Playgroud)
T Int我实现的一个实例:
import T
import A (av)
instance T Int where
v = 0
main = putStrLn (av ++ show v)
Run Code Online (Sandbox Code Playgroud)
还有一个模块,我想使用一个值,也有一个实例T Int.
module A where
import T
instance T Int where
v = 0
av = "value from A"
Run Code Online (Sandbox Code Playgroud)
问题是这不起作用:
$ runghc Main.hs
Main.hs:4:9:
Duplicate instance declarations:
instance T Int -- Defined at Main.hs:4:9-13
instance T Int -- Defined at A.hs:3:9-13
Run Code Online (Sandbox Code Playgroud)
Haskell抱怨同一个实例有2个声明.我如何告诉他不要从中导入实例B,或统一这两个实例,或仅使用实例Main …
我有一个A带有静态内部类的类,名为B:
import static A.B.*;
class A {
static class B {
static int x;
static int y;
}
public static void main(String[] args) {
System.out.println(x);
}
}
Run Code Online (Sandbox Code Playgroud)
我想静态导入所有内容B,但它不会工作:
$ javac A.java
A.java:1: package A does not exist
import static A.B.*;
^
A.java:9: cannot find symbol
symbol : variable x
location: class A
System.out.println(x);
^
2 errors
Run Code Online (Sandbox Code Playgroud)
为什么?
我曾经认为,直观地说,Java中的构造函数是创建对象的东西,并且在构造函数返回之前没有任何东西可以触及该对象.但是,我一遍又一遍地证明了这一点:
this所有这些事实都违反了我对构造函数的直觉.
我不能再自信地说出构造函数在Java中实际做了什么,或者它的用途是什么.如果我正在使用所有最终字段创建一个简单的DTO ,那么我可以理解构造函数的用途是什么,因为这与C中的结构完全相同,除非它无法修改.除此之外,我不知道在Java中可以可靠地使用哪些构造函数.它们只是一种惯例/句法糖吗?(即如果只有工厂为你初始化对象,你只会有X x = new X(),然后修改每个字段x以使它们具有非默认值 - 鉴于上述3个事实,这几乎等同于Java实际上是如何)
我可以命名两个实际上由构造函数保证的属性:如果我这样做X x = new X(),那么我知道它x是一个实例,X但不是它的子类X,并且它的最终字段已完全初始化.你可能想说你知道X完成的构造函数并且你有一个有效的对象,但是如果你传递X给另一个线程这是不真实的- 另一个线程可能会看到未初始化的版本(即你刚才所说的与没有什么不同的叫工厂的保证.建造者实际保证的其他什么属性?
我正在尝试在Eclipse Java项目中重命名一个方法,但它似乎重命名了每个具有相同名称的方法.(也许我误解了这个功能是什么 - 也许它只是使用sed?)
这是一个简化的例子:
public class C1 {
interface Listener {
void f();
}
public C2.Listener c2l = new C2.Listener() {
public void f() {
}
};
}
Run Code Online (Sandbox Code Playgroud)
public class C2 {
interface Listener {
void f();
}
}
Run Code Online (Sandbox Code Playgroud)
如果我选择了f方法C2,并从"重构"菜单中选择"重命名"以将其重命名为g,C1.Listener.f则也会重命名,结果C1更改为:
public class C1 {
interface Listener {
void g();
}
public C2.Listener c2l = new C2.Listener() {
public void g() {
}
};
}
Run Code Online (Sandbox Code Playgroud)
我预计它会改为:
public class …Run Code Online (Sandbox Code Playgroud) 我正在尝试在JUnit中进行一个简单的示例测试,测试两件事,然后拆卸.
import org.junit.*;
public class TestFoobar {
@Test
public void testOneThing() {
// Code that tests one thing
}
@Test
public void testAnotherThing() {
// Code that tests another thing
}
@AfterClass
@Test
public void tearDownClass() throws Exception {
// Code executed after the last test method
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行它时,它崩溃说tearDownClass必须是静态的:
$ javac TestFoobar.java -cp junit-4.8.2.jar
$ java -cp junit-4.8.2.jar:. org.junit.runner.JUnitCore TestFoobar
JUnit version 4.8.2
.E
Time: 0.01
There was 1 failure:
1) initializationError(TestFoobar)
java.lang.Exception: Method tearDownClass() should be …Run Code Online (Sandbox Code Playgroud) 我正在尝试select在循环中使用a 来接收消息或超时信号.如果收到超时信号,则循环应该中止:
package main
import ("fmt"; "time")
func main() {
done := time.After(1*time.Millisecond)
numbers := make(chan int)
go func() {for n:=0;; {numbers <- n; n++}}()
for {
select {
case <-done:
break
case num := <- numbers:
fmt.Println(num)
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,它似乎没有停止:
$ go run a.go
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 …Run Code Online (Sandbox Code Playgroud) 当我把光标放在vim的括号上时:
f = (\x y -> x+y)
^
Run Code Online (Sandbox Code Playgroud)
键入%将光标移动到匹配的括号:
f = (\x y -> x+y)
^
Run Code Online (Sandbox Code Playgroud)
键入%d将删除括号及其中的所有内容
f =
^
Run Code Online (Sandbox Code Playgroud)
但是当我有嵌套括号时:
g = (\(x,y) -> x+y)
^
Run Code Online (Sandbox Code Playgroud)
键入%使其跳转到关闭的内括号而不是匹配的括号:
g = (\(x,y) -> x+y)
^
Run Code Online (Sandbox Code Playgroud)
并%d具有相同的不匹配行为:
g = (\(x,y) -> x+y)
^
Run Code Online (Sandbox Code Playgroud)
变
g = -> x+y)
^
Run Code Online (Sandbox Code Playgroud)
为什么?如何使其正确匹配?
奇怪的是,匹配括号的视觉突出显示确实有效:

这是如何工作的,但匹配%不是吗?有两个大脑吗?
看看这个简单的JSON系统,该系统由面向用户的服务组成,后端与后端通信:
import java.util.*;
import com.json.parsers.*;
import org.json.simple.*;
class Main{static {
String s = new java.util.Scanner(System.in).useDelimiter("\\A").next();
UserFacingService.handleMessage(s);
}}
class UserFacingService {
static void handleMessage(String s) {
JSONParser parser = JsonParserFactory.getInstance().newJsonParser();
Map js = parser.parseJson(s);
if (commandAllowed(js))
Backend.handleMessage(s);
else
System.out.println("unauthorized!");
}
static boolean commandAllowed(Map js) {
if (js.get("cmd").equals("bad stuff"))
return false;
return true;
}
}
class Backend {
static void handleMessage(String s) {
JSONObject js = (JSONObject) JSONValue.parse(s);
String cmd = (String)js.get("cmd");
if (cmd.equals("bad stuff")) {
doBadStuff();
} else if (cmd.equals("ping")) { …Run Code Online (Sandbox Code Playgroud) 自从我开始编写Java以来,我注意到每个人都在使用&&而||不是&和|.这是什么原因?我一直在使用&&,||因为我不知道你可以使用&和|布尔.
class A{static{
boolean t = true;
boolean f = false;
System.out.println("&ff " + (f&&f) + " " + (f&f));
System.out.println("&ft " + (f&&t) + " " + (f&t));
System.out.println("&tf " + (t&&f) + " " + (t&f));
System.out.println("&tt " + (t&&t) + " " + (t&t));
System.out.println("|ff " + (f||f) + " " + (f|f));
System.out.println("|ft " + (f||t) + " " + …Run Code Online (Sandbox Code Playgroud) java boolean bit-manipulation operators conditional-statements
我正在尝试运行这个简单的Python代码(a.py):
# list test
import time
class List(object):
class Cons(object):
def __init__(self, x, tail):
self.x= x
self.tail=tail
class Nil(object):
def __init__(self):
pass
@classmethod
def cons(cls, cons):
return List(cons, None)
@classmethod
def nil(cls, nil):
return List(None, nil)
def __init__(self, cons, nil):
self.cons = cons
self.nil = nil
def replicate(n, x):
return List.nil(List.Nil()) if n == 0 else List.cons(List.Cons(x,replicate(n-1,x)))
t1 = time.time()
List.cons(List.Cons(object(), List.nil(List.Nil())))
t2 = time.time() - t1
print t2
Run Code Online (Sandbox Code Playgroud)
但它不起作用:
$ ./a.py
Version: ImageMagick 6.8.8-10 Q16 x86_64 2014-04-08 http://www.imagemagick.org
Copyright: …Run Code Online (Sandbox Code Playgroud) java ×6
concurrency ×2
haskell ×2
namespaces ×2
security ×2
android ×1
bash ×1
boolean ×1
compilation ×1
constructor ×1
eclipse ×1
escaping ×1
go ×1
goroutine ×1
import ×1
inheritance ×1
json ×1
junit ×1
operators ×1
python ×1
select ×1
static ×1
typeclass ×1
unicode ×1
unit-testing ×1
vim ×1