Adr*_*zza 3 struct object julia
我正在从使用 Python 转向 Julia,并想创建一个这样的对象:
class myObject():
def __init__(inputA,inputB):
self.x = inputA;
self.y = inputB;
self.z = x*y;
Run Code Online (Sandbox Code Playgroud)
我知道在 Julia 中我们使用struct
但不确定如何在不z
手动设置的情况下实现上述功能(内部构造函数之外)。我该怎么做?
您可以将其作为内部构造函数执行:
struct A
x::Int
y::Int
z::Int
# Inner constructor
A(x, y) = new(x, y, x*y)
end
Run Code Online (Sandbox Code Playgroud)
或外部构造函数:
struct B
x::Int
y::Int
z::Int
end
# Outer constructor
B(x, y) = B(x, y, x*y)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
42 次 |
最近记录: |