在值对象上使用方法链接模式是否可接受/良好实践(比如,返回一个新对象而不是这个)?是否存在实施此解决方案的案例?
我想不出任何弊端,但我想听听你的观点.
在javascript中有两种创建对象的方法:
例如,第一个是
FooType = function() {
this.hello = function() { alert("hello"); };
};
foo = new FooType();
foo.hello();
Run Code Online (Sandbox Code Playgroud)
第二是
fooFactory = function() {
return {
hello : function() { alert("hello"); }
};
};
foo = fooFactory();
foo.hello();
Run Code Online (Sandbox Code Playgroud)
(为帖子写的代码.不保证正确)
除了将此绑定到全局对象的错误风险之外,这两种方法完全等效(还考虑原型继承等)吗?
我认为复杂的问题,但研究OWL开辟了生活,宇宙和一切的新视角.我在这里要哲学.
我正在尝试实现一个C类,它是B的子类,而B又是C的子类.只是为了好玩,你知道......
所以这就是
>>> class A(object): pass
...
>>> class B(A): pass
...
>>> class C(B): pass
...
>>> B.__bases__
(<class '__main__.A'>,)
>>> B.__bases__ = (C,)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: a __bases__ item causes an inheritance cycle
>>>
Run Code Online (Sandbox Code Playgroud)
显然,python是聪明的,禁止这个.但是,在OWL中,可以将两个类定义为相互子类.问题是:为什么在OWL(不是编程语言)中允许这种情况并且在编程语言中不允许这是什么令人难以置信的解释?
在以下行中
Graph<Number,Number> ig = Graphs.<Number,Number>synchronizedDirectedGraph(
new DirectedSparseMultigraph<Number,Number>());
Run Code Online (Sandbox Code Playgroud)
你能解释一下是什么Graphs.<Number,Number>synchronizedDirectedGraph意思吗?它看起来像一个方法Graphs.synchronizedDirectedGraph的调用,但点后面的模板式东西困惑我(至少由于我的C++背景).
假设我在python中有这个对象
class Foo:
def __init__(self, val):
self.val = val
Run Code Online (Sandbox Code Playgroud)
和这两个变量
a=Foo(5)
b=a
Run Code Online (Sandbox Code Playgroud)
二者b并a指的同一实例Foo(),所以任何修改属性.val将被同等地可见并且作为同步a.val和b.val.
>>> b.val
5
>>> b.val=3
>>> a.val
3
Run Code Online (Sandbox Code Playgroud)
现在假设我想说a=Foo(7).这将创建另一个Foo实例,所以现在a并且b是独立的.
我的问题是:有没有办法b自动重新读取新的Foo()实例,而不使用中间代理对象?用我提出的方法显然不可能,但也许有一些我不知道的魔法.
在 Fortran 中,您可以将函数/子例程 A 作为参数传递给另一个函数/子例程 B,但是您能否存储 A 以供以后检索和使用?
例如,这在 C 中是允许的
int foo(float, char, char) { /*whatever*/};
int (*pointerToFunction)(float, char, char);
pointerToFunction = foo;
Run Code Online (Sandbox Code Playgroud)
在 Fortran 中,您可以将子程序作为参数传递
subroutine foo
! whatever
end subroutine foo
subroutine bar(func)
call func
end subroutine bar
program x
call bar(foo)
end program
Run Code Online (Sandbox Code Playgroud)
但是如何以与 C 类似的方式存储 foo 的地址?
这很有趣..我正在尝试从openstreetmap读取地理查找数据.执行查询的代码如下所示
params = urllib.urlencode({'q': ",".join([e for e in full_address]), 'format': "json", "addressdetails" : "1"})
query = "http://nominatim.openstreetmap.org/search?%s" % params
print query
time.sleep(5)
response = json.loads(unicode(urllib.urlopen(query).read(), "UTF-8"), encoding="UTF-8")
print response
Run Code Online (Sandbox Code Playgroud)
对Zürich的查询在UTF-8数据上进行了正确的URL编码.这里没有奇迹.
http://nominatim.openstreetmap.org/search?q=Z%C3%BCrich%2CSWITZERLAND&addressdetails=1&format=json
Run Code Online (Sandbox Code Playgroud)
当我打印响应时,带有变音符号的u被编码为latin1(0xFC)
[{u'display_name': u'Z\xfcrich, Bezirk Z\xfcrich, Z\xfcrich, Schweiz, Europe', u'place_id': 588094, u'lon': 8.540443
Run Code Online (Sandbox Code Playgroud)
但这是无稽之谈,因为openstreetmap以UTF-8返回JSON数据
Connecting to nominatim.openstreetmap.org (nominatim.openstreetmap.org)|128.40.168.106|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Date: Wed, 26 Jan 2011 13:48:33 GMT
Server: Apache/2.2.14 (Ubuntu)
Content-Location: search.php
Vary: negotiate
TCN: choice
X-Powered-By: PHP/5.3.2-1ubuntu4.7
Access-Control-Allow-Origin: *
Content-Length: 3342
Keep-Alive: …Run Code Online (Sandbox Code Playgroud) 假设我想在CUDA中对设备执行异步memcpy主机,然后立即运行内核.如果异步传输已完成,我如何在内核中进行测试?
我发现这段代码出乎意料
module testmodule
integer, parameter :: LCHARS = 50
contains
subroutine init()
call foobar("foobar")
end subroutine
subroutine foobar(s)
character(len=*), intent(in) :: s
call bar(s)
end subroutine
subroutine bar(str)
character(len=LCHARS), intent(in) :: str
print *, str
end subroutine
end module
program foo
use testmodule
call init()
end program
Run Code Online (Sandbox Code Playgroud)
此代码打印依赖于编译器的垃圾.
我发现问题在于我正在跳过一个带有len=*字符串参数的例程,然后将其传递给字符串参数的指定长度的例程.
正是在幕后发生了什么,标准中描述的这种行为在哪里?我是否应该避免为字符例程参数指定长度,因为这种行为可能在任何时候发生而没有警告?
我试图理解boost shared_ptr类的底层设计.我想把它"移植"到fortran(不要问).我理解的一件事是引用计数由shared_count类保存.这提示我一个问题.我很久没有使用过C++,也从未使用过boost.
假设我分配了一个类X的单个实例,然后将其传递给两个不同的shared_ptr实例.据我了解,每shared_ptr的实例不知道其他任何东西,因此,这两shared_ptr实例指的是相同的X实例,同时保持1引用计数,如果一个shared_ptr离开的范围,而其他没有, X对象将被删除(因为refcount降为零),剩下的shared_ptr将有一个悬空指针.为了保持shared_ptr引用计数,您必须从另一个shared_ptr创建shared_ptr.
我对吗 ?如果没有,那么如何提升跟踪哪些shared_ptrs引用了一个对通过shared_ptrs引用的事实一无所知的类?