我通常以“本能”方式通过函数进行编程,但我目前的问题可以通过对象轻松解决,所以我继续使用此方法。例如,我试图找到一种方法来给对象一个构造方法,相当于init()在python中。在rebol中是否有对象构造函数
我查看了http://www.rebol.com/docs/core-fr/fr-index.html文档,但找不到任何相关内容。
我通常以“本能”方式通过函数进行编程,但我目前的问题可以通过对象轻松解决,所以我继续使用此方法。例如,我试图找到一种方法来给对象一个构造方法,相当于init()在python中。在rebol中是否有对象构造函数
我查看了http://www.rebol.com/docs/core-fr/fr-index.html文档,但找不到任何相关内容。
实际上,通过再次读Rebol的核心文档(我只是跟着好老的建议:“不懂法文手册”),还有另一种方式来实现一个构造函数,很简单:
http://www.rebol.com/docs/core-fr/fr-rebolcore-10.html#section-8
当然它也是英语手册:
http://www.rebol.com/docs/core23/rebolcore-10.html#section-7
=>
使用自变量的另一个例子是克隆 本身的功能:
person: make object! [
name: days-old: none
new: func [name' birthday] [
make self [
name: name'
days-old: now/date - birthday
]
]
]
lulu: person/new "Lulu Ulu" 17-May-1980
print lulu/days-old
7366
我觉得这很方便,这样一来,构造位于对象内。这个事实使对象更加自给自足。
我刚刚实施,成功地为一些地质东西,它工作得很好:
>> source orientation
orientation: make object! [
matrix: []
north_reference: "Nm"
plane_quadrant_dip: ""
new: func [{Constructor, builds an orientation object! based on a measurement, as given by GeolPDA device, a rotation matrix represented by a suite of 9 values} m][
make self [
foreach [a b c] m [append/only matrix to-block reduce [a b c]]
a: self/matrix/1/1
b: self/matrix/1/2
c: self/matrix/1/3
d: self/matrix/2/1
e: self/matrix/2/2
f: self/matrix/2/3
g: self/matrix/3/1
h: self/matrix/3/2
i: self/matrix/3/3
plane_normal_vector: reduce [matrix/1/3
matrix/2/3
matrix/3/3
]
axis_vector: reduce [self/matrix/1/2
self/matrix/2/2
self/matrix/3/2
]
plane_downdip_azimuth: azimuth_vector plane_normal_vector
plane_direction: plane_downdip_azimuth - 90
if (plane_direction < 0) [plane_direction: plane_direction - 180]
plane_dip: arccosine (plane_normal_vector/3)
case [
((plane_downdip_azimuth > 315) or (plane_downdip_azimuth <= 45)) [plane_quadrant_dip: "N"]
((plane_downdip_azimuth > 45) and (plane_downdip_azimuth <= 135)) [plane_quadrant_dip: "E"]
((plane_downdip_azimuth > 135) and (plane_downdip_azimuth <= 225)) [plane_quadrant_dip: "S"]
((plane_downdip_azimuth > 225) and (plane_downdip_azimuth <= 315)) [plane_quadrant_dip: "W"]
]
line_azimuth: azimuth_vector axis_vector
line_plunge: 90 - (arccosine (axis_vector/3))
]
]
repr: func [][
print rejoin ["Matrix: " tab self/matrix
newline
"Plane: " tab
north_reference to-string to-integer self/plane_direction "/" to-string to-integer self/plane_dip "/" self/plane_quadrant_dip
newline
"Line: " tab
rejoin [north_reference to-string to-integer self/line_azimuth "/" to-string to-integer self/line_plunge]
]
]
trace_te: func [diagram [object!]][
len_queue_t: 0.3
tmp: reduce [
plane_normal_vector/1/(square-root (((plane_normal_vector/1 ** 2) + (plane_normal_vector/2 ** 2))))
plane_normal_vector/2/(square-root (((plane_normal_vector/1 ** 2) + (plane_normal_vector/2 ** 2))))
]
O: [0 0]
A: reduce [- tmp/2
tmp/1
]
B: reduce [tmp/2 0 - tmp/1]
C: reduce [tmp/1 * len_queue_t
tmp/2 * len_queue_t
]
L: reduce [- axis_vector/1 0 - axis_vector/2]
append diagram/plot [pen black]
diagram/trace_line A B
diagram/trace_line O C
diagram/trace_line O L
]
]
>> o: orientation/new [0.375471 -0.866153 -0.32985 0.669867 0.499563 -0.549286 0.640547 -0.0147148 0.767778]
>> o/repr
Matrix: 0.375471 -0.866153 -0.32985 0.669867 0.499563 -0.549286 0.640547 -0.0147148 0.767778
Plane: Nm120/39/S
Line: Nm299/0
这种方式的另一个优点是,通过“新”方法定义的变量直接属于对象“实例”(我碰到了一些麻烦,用其他方法,不得不提及自己/有时候,不得不初始化变量)。
中有雷博尔没有特殊的构造函数,但是如果你需要它的对象在规范块创建写入特设初始化代码的可能性。例如:
a: context [x: 123]
b: make a [
y: x + 1
x: 0
]
所以,如果你在基本对象约定定义自己的“构造”的功能,你可以把它在创建规范块。如果你想让它自动的,你可以用在函数,就像这样:
a: context [
x: 123
init: func [n [integer!]][x: n]
]
new-a: func [n [integer!]][make a [init n]]
b: new-a 456
更健壮的(但位长)的新一版本将避免传递的参数可能的碰撞到初始化与对象自己的话说就是:
new-a: func [n [integer!] /local obj][
also
obj: make a []
obj/init n
]
你也可以写一个更通用的新功能,将采取基本对象作为第一个参数,自动调用酷龙后构造按约定功能对象,但以通用方式支持可选的构造函数参数则更加棘手。
记住是Rebol的对象模型是基于原型(VS基于类在Python和大多数其他OOP语言),因此“构造函数”功能得到复制每个创建的新对象。如果您要创建大量对象,则可能需要避免此类成本。
据我所知,没有使用对象构造函数的正式方法/约定,如init()
。当然还有一个构建衍生对象的内置方法:
make prototype [name: "Foo" description: "Bar"]
; where type? prototype = object!
我最好的建议是定义检查的构造方法的对象的功能,然后应用该方法,这里有一个这样的功能,我“已经proposed previously:
new: func [prototype [object!] args [block! none!]][
prototype: make prototype [
if in self 'new [
case [
function? :new [apply :new args]
block? :new [apply func [args] :new [args]]
]
]
]
]
用法是相当简单:
thing: context [
name: description: none
new: [name: args/1 description: args/2]
]
derivative: new thing ["Foo" "Bar"]
012:如果一个原型对象具有
new
值,则它会在衍生物对象的构造应用于
请注意,此方法适用于Rebol 2和3。
好的:如果需要的话,我们需要的是一个对象中构造函数方法的适当约定。 – Pierre
我想了解OO如何在REBOL中工作。确实是典型的。昨天我碰到这个页面,这启发了我下面的经典OO模型,没有职能重叠:
;---- Generic function for class or instance method invocation ----;
invoke: func [
obj [object!]
fun [word!]
args [block!]
][
fun: bind fun obj/.class
;---- Class method names start with a dot and instance method names don't:
unless "." = first to-string fun [args: join args obj]
apply get fun args
]
;---- A class definition ----;
THIS-CLASS: context [
.class: self ; the class refers to itself
;---- Class method: create new instance ----;
.new: func [x' [integer!] /local obj] [
obj: context [x: x' .class: none] ; this is the object definition
obj/.class: self/.class ; the object will refer to the class
; it belongs to
return obj
]
;---- An instance method (last argument must be the instance itself) ----;
add: func [y obj] [
return obj/x + y
]
]
然后,你可以这样做:
;---- First instance, created from its class ----;
this-object: THIS-CLASS/.new 1
print invoke this-object 'add [2]
;---- Second instance, created from from a prototype ----;
that-object: this-object/.class/.new 2
print invoke that-object 'add [4]
;---- Third instance, created from from a prototype in another way ----;
yonder-object: invoke that-object '.new [3]
print invoke yonder-object 'add [6]
;---- Fourth instance, created from from a prototype in a silly way ----;
silly-object: yonder-object/.class/.class/.class/.class/.new 4
print silly-object/.class/add 8 silly-object
print this-object/.class/add 8 silly-object
print THIS-CLASS/add 8 silly-object
(它可以在REBOL 2,打印3,6,9,12,12,12)。几乎没有任何开销。也许不难找到其他十几种解决方案。确实,这是真正的问题:有太多的方法来做到这一点。 (也许我们最好使用LoyalScript。)
是的,你们最终都会得到非常相似的解决方案。初始化还是新建?这就是问题所在,要命名构造方法... 当然,如果要生成大量的对象,绝对应该避免使用这些方法。在我目前的问题中,我实际上正在考虑一些对象,甚至没有一个。 – Pierre