在自定义类型构造时自动 运行 显示方法

show method automatically running upon custom type construction

每次我为自定义类型调用构造函数时,该类型的 show 方法 运行s。我不知道为什么。可重现的例子如下:

我有一个模块:

module ctbTestModule1

import  Base.show

export  MyType1

type MyType1
    function MyType1()
        new()
    end
end

function show(io::IO, a::MyType1)
    println("hello world")
end

end

我在 REPL 中打开一个新的 julia 会话并输入:

using ctbTestModule1
z = MyType1()

当我 运行 行 z = MyType1():

时,以下内容打印到控制台
hello world

这里如何调用show方法?它肯定不会在内部构造函数中调用...

REPL(读取-评估-打印循环)评估并打印每个语句。

您描述的是正常行为。

运行 z = 1 在您的 REPL 中,打印输出将是 1.

同样,您的 zMyType1,显示为 hello world

如果你想抑制 REPL 中的输出,用分号结束你的语句 ;:

z = MyType1();

所以,回答你的问题:是的,如果你在 REPL 中创建一个类型的实例,结果将通过调用该类型的 show() 函数来显示.