Julia: OOP or not

后端 未结 6 1077
难免孤独
难免孤独 2021-02-01 00:46

I\'m working on Juno with Julia.

I don\'t know if Julia supports OOP or not.

For example, is there something like class or struct of c+

6条回答
  •  后悔当初
    2021-02-01 01:36

    When in doubt, read the documentation...

    https://docs.julialang.org/en/v1/manual/types/#Composite-Types-1

    Long story short:

    struct MyType
        a::Int64
        b::Float64
    end
    
    x = MyType(3, 4)
    
    x.a
    

    EDIT: Methods are defined outside the type definition, e.g.

    function double(x::MyType)
        x.a *= 2
    end
    

    Methods do not live inside the type, as they would do in C++ or Python, for example. This allows one of the key features of Julia, multiple dispatch, to work also with user-defined types, which are on exactly the same level as system-defined types.

提交回复
热议问题