说我有一些课,比如Foo:
public class Foo {
private Integer x;
private Integer y;
public Foo(Integer x, Integer y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我希望添加一个构造函数,该构造函数将表示Foo的字符串作为其参数,例如Foo("1 2")将构造一个x = 1且y = 2的Foo.由于我不想在原始构造函数中复制逻辑,我希望能够做到这样的事情:
public Foo(string stringRepresentation) {
Integer x;
Integer y;
// ...
// Process the string here to get the values of x and y.
// ...
this(x, y);
}
Run Code Online (Sandbox Code Playgroud)
但是,Java在调用this(x,y)之前不允许语句.是否有一些可接受的解决方法?
<aside>在HTML 5中使用标记时,我们是否需要为其添加float:right(或left)属性?因为大多数时候,我认为它被用作侧边栏.
这是基本问题,但我只是想弄清楚使用它的正确方法.
考虑:
class Foo
{
static Foo()
{
// Static initialisation
}
}
Run Code Online (Sandbox Code Playgroud)
为什么()需要static Foo() {...}?静态构造函数必须始终是无参数的,为什么要这么麻烦?它们是否有必要避免一些解析器歧义,或者只是为了保持与常规无参数构造函数的一致性?
由于它看起来非常像初始化块,我经常发现自己意外地将它们排除在外,然后不得不考虑几秒钟的错误.如果他们可以以同样的方式被淘汰将是很好的.
来自Python文档re.compile():
注意传递给re.match(),re.search()或re.compile()的最新模式的编译版本被缓存,因此一次只使用几个正则表达式的程序不必担心定期编译表达式.
但是,在我的测试中,这个断言似乎没有成功.在对重复使用相同模式的以下片段进行计时时,编译版本仍然比未编译版本(应该被缓存)快得多.
我在这里找不到能解释时差的东西吗?
import timeit
setup = """
import re
pattern = "p.a.t.t.e.r.n"
target = "p1a2t3t4e5r6n"
r = re.compile(pattern)
"""
print "compiled:", \
min(timeit.Timer("r.search(target)", setup).repeat(3, 5000000))
print "uncompiled:", \
min(timeit.Timer("re.search(pattern, target)", setup).repeat(3, 5000000))
Run Code Online (Sandbox Code Playgroud)
结果:
compiled: 2.26673030059
uncompiled: 6.15612802627
Run Code Online (Sandbox Code Playgroud) 我试图绘制以下函数:
f(x) = 0 if x is rational else 1 # so 1 if x is irrational
Run Code Online (Sandbox Code Playgroud)
我的计划是使用python和matplotlib.你如何在Python中生成随机的无理数?
我想使用CSS来设置两个不同的输入,但在我的CSS文件中只使用两个类的一个声明.这是我试过的
HTML:
<input type="text" name="data_start" id="data_start" class="data_start" />
<input type="text" name="data_end" id="data_end" class="data_end" />
Run Code Online (Sandbox Code Playgroud)
CSS:
.data_start .data_end { width:68px; margin-left:5px; }
Run Code Online (Sandbox Code Playgroud)
如果我使用上面的代码,我的输入不会被设置样式,但如果我在我的CSS文件中执行以下操作,它将工作:
.data_start { width:68px; margin-left:5px; }
.data_end { width:68px; margin-left:5px; }
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么第一个CSS语句不起作用?
python ×2
c# ×1
class ×1
constructor ×1
css ×1
html5 ×1
java ×1
math ×1
performance ×1
python-2.7 ×1
regex ×1
tostring ×1