朱莉娅:OOP 与否 [英] Julia: OOP or not

查看:29
本文介绍了朱莉娅:OOP 与否的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和 Julia 一起开发 Juno.

I'm working on Juno with Julia.

我不知道 Julia 是否支持 OOP.

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

例如,有没有类似c++的classstruct之类的东西?

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

如何用数据或函数等成员声明?

How to declare it with members such as a data or a function?

推荐答案

如有疑问,请阅读文档...

When in doubt, read the documentation...

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

长话短说:

struct MyType
    a::Int64
    b::Float64
end

x = MyType(3, 4)

x.a

方法定义在类型定义之外,例如

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

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

方法存在于类型中,例如它们在 C++ 或 Python 中的行为.这使得 Julia 的关键特性之一,即多重分派,也可以与用户定义的类型一起使用,这些类型与系统定义的类型处于完全相同的级别.

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.

这篇关于朱莉娅:OOP 与否的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆