gpt4 book ai didi

struct - Julia 中的不完整初始化

转载 作者:行者123 更新时间:2023-12-04 01:23:28 26 4
gpt4 key购买 nike

我想在 Julia 中初始化数据类型的一部分,我目前有:

mutable struct Foo
bar::Int
baz::Int
maz::Int
Foo(maz=2)=new(maz)
end

foo=Foo()
println(foo)

然而,这会创建一个对象,它是 Foo(2, 0, 0)

我该怎么做才能得到 Foo(0, 0, 2)

注意:我宁愿不必进行完整的初始化

最佳答案

使用 new()(详见 here):

julia> mutable struct Foo
bar::Int
baz::Int
maz::Int
function Foo(maz=2)
foo = new()
foo.maz = maz
return foo
end
end

julia> foo=Foo()
Foo(139985835568976, 8, 2)

请注意,barbaz 字段未设置为 0,而是未初始化。

关于struct - Julia 中的不完整初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62248671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com