Julia 的内部构造函数

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手动设置的情况下实现上述功能(内部构造函数之外)。我该怎么做?

fre*_*kre 5

您可以将其作为内部构造函数执行:

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)

手册构造函数部分应涵盖所有内容。