Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在Julia中使用内部构造函数方法?_Julia - Fatal编程技术网

为什么在Julia中使用内部构造函数方法?

为什么在Julia中使用内部构造函数方法?,julia,Julia,如果我理解的话,内部构造函数方法的价值在于我可以将它们作为常规构造函数使用,但需要对值进行一些额外的更改 例如,使用普通构造函数不可能获取构造函数参数并向其添加数字1,但使用内部构造函数,这是可能的?内部构造函数允许您替换默认构造函数。例如: julia> struct A x::Int A(a::Int,b::Int)=new(a+b) end julia> A(3) ERROR: MethodError: no method match

如果我理解的话,内部构造函数方法的价值在于我可以将它们作为常规构造函数使用,但需要对值进行一些额外的更改


例如,使用普通构造函数不可能获取构造函数参数并向其添加数字1,但使用内部构造函数,这是可能的?

内部构造函数允许您替换默认构造函数。例如:

julia> struct A
       x::Int
       A(a::Int,b::Int)=new(a+b)
       end

julia> A(3)
ERROR: MethodError: no method matching A(::Int64)

julia> A(3,5)
A(8)
请注意,如果未定义内部构造函数,则它实际上与默认参数集一起存在。但是,添加外部构造函数不会覆盖内部构造函数的行为:

julia> struct B
       x::Int
       end

julia> B(a::Int,b::Int)=B(a+b);

julia> B(3)
B(3)

julia> B(3,5)
B(8)