简体   繁体   English

朱莉娅:OOP 与否

[英]Julia: OOP or not

I'm working on Juno with Julia.我正在和朱莉娅一起研究朱诺。

I don't know if Julia supports OOP or not.我不知道 Julia 是否支持 OOP。

For example, is there something like class or struct of c++?例如,是否有类似 c++ 的classstructclass东西?

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 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, eg编辑:方法是在类型定义之外定义的,例如

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.方法并不存在于类型中,例如它们在 C++ 或 Python 中存在。 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.这允许 Julia 的关键特性之一,多重分派,也可以与用户定义的类型一起使用,这些类型与系统定义的类型处于完全相同的级别。

Julia is not object-oriented in the full sense because you cannot attach methods to Julia's objects ("types"). Julia 并不是完全意义上的面向对象,因为您不能将方法附加到 Julia 的对象(“类型”)。 The types do seem very similar to objects though.尽管这些类型看起来与对象非常相似。 However, since they do not have their own associated methods and there is no inheritance the objects themselves don't do the acting.但是,由于它们没有自己的关联方法并且没有继承,因此对象本身不执行操作。 Instead you have functions which act on the objects.相反,您拥有作用于对象的函数。

The difference is ball.checkCollision() vs checkCollision(ball,Walls).区别在于 ball.checkCollision() 与 checkCollision(ball,Walls)。 In reality it's not that big of a deal.实际上,这没什么大不了的。 You can make something like inheritance by having a type have a field of another type, and multiple dispatch lets you write functions which do different things based on the objects you give them, which can be almost like object methods.您可以通过让一个类型具有另一个类型的字段来实现类似继承,而多分派让您可以编写函数,根据您提供的对象执行不同的操作,这几乎就像对象方法一样。 The real difference is where you save the function and types in a file.真正的区别在于您将函数和类型保存在文件中的位置。 So you can do a kind of quasi-objected-oriented style in Julia, but it's still distinctly different than OOP languages.所以你可以在 Julia 中做一种准面向对象的风格,但它仍然与 OOP 语言有明显的不同。

I would like to mention this worthfull conversation within Julia users group Julia and Object-Oriented Programming .我想在 Julia 用户组Julia 和 Object-Oriented Programming 中提及这个有价值的对话。
For me Julia is not like a conventional OO language, and I always like to think of Julia, as more a Method Oriented language that an Object Oriented one, that is because if you try to create an structure of encapsulated data and functionality in Julia, soon you will get yourself into trouble.对我来说,Julia 不像传统的 OO 语言,我总是喜欢将 Julia 视为一种面向方法的语言,而不是面向对象的语言,这是因为如果您尝试在 Julia 中创建封装数据和功能结构,很快你就会陷入困境。

The answer is that Julia is closer to c rather than c++.答案是 Julia 更接近 c 而不是 c++。 Thus, OOP is limited in Julia having only c::struct rather than c++::class.因此,OOP 在 Julia 中受到限制,只有 c::struct 而不是 c++::class。 Hence, it's just data encapsulation or data structure or customization of Types rather than true OOP objects, in the strict sense.因此,严格来说,它只是数据封装或数据结构或类型的定制,而不是真正的 OOP 对象。

Yes.是的。 You just can't inherit from concrete "classes" with fields, only from abstract ones.您不能从具有字段的具体“类”继承,只能从抽象类继承。 This is because the memory layout of a concrete struct in Julia is part of its interface, and being able to subclass it by adding fields would break the Liskov substitution principle.这是因为 Julia 中具体结构的内存布局是其接口的一部分,并且能够通过添加字段对其进行子类化会破坏 Liskov 替换原则。

However, Functions with multiple dispatch are a strict generalization of methods with single dispatch and are strictly more polymorphic.但是,具有多重分派的函数是具有单一分派的方法的严格泛化,并且更加多态。 You just have to define them outside of a class definition, because they can "belong" to multiple objects.您只需要在类定义之外定义它们,因为它们可以“属于”多个对象。

Traditional methods in an OO language are a special case of Julia functions where you only have dispatching based on the first argument. OO 语言中的传统方法是 Julia 函数的一个特例,您只能根据第一个参数进行调度。

I'm no expert on the language but my understanding is: Yes..and no.我不是这门语言的专家,但我的理解是:是的……也不是。

It has the equivalent of classes and structs, however there are no methods on those objects other than a single constructor.它具有类和结构的等价物,但是除了单个构造函数之外,这些对象上没有任何方法。

In mainstream object oriented languages, such as C++, Java, Python and Ruby, composite types also have named functions associated with them, and the combination is called an “object”.在主流的面向对象语言中,如 C++、Java、Python 和 Ruby,复合类型也有与之关联的命名函数,这种组合称为“对象”。 In purer object-oriented languages, such as Python and Ruby, all values are objects whether they are composites or not.在更纯粹的面向对象语言中,例如 Python 和 Ruby,所有值都是对象,无论它们是否复合。 In less pure object oriented languages, including C++ and Java, some values, such as integers and floating-point values, are not objects, while instances of user-defined composite types are true objects with associated methods.在不太纯的面向对象语言中,包括 C++ 和 Java,一些值,例如整数和浮点值,不是对象,而用户定义的复合类型的实例是具有关联方法的真正对象。 In Julia, all values are objects, but functions are not bundled with the objects they operate on.在 Julia 中,所有值都是对象,但函数并不与它们操作的对象捆绑在一起。 This is necessary since Julia chooses which method of a function to use by multiple dispatch, meaning that the types of all of a function's arguments are considered when selecting a method, rather than just the first one (see Methods for more information on methods and dispatch).这是必要的,因为 Julia 选择通过多个分派使用函数的哪个方法,这意味着在选择方法时会考虑函数的所有参数的类型,而不仅仅是第一个(有关方法和分派的更多信息,请参阅方法)。 Thus, it would be inappropriate for functions to “belong” to only their first argument.因此,函数只“属于”它们的第一个参数是不合适的。 Organizing methods into function objects rather than having named bags of methods “inside” each object ends up being a highly beneficial aspect of the language design.将方法组织成函数对象而不是在每个对象“内部”命名方法包最终成为语言设计的一个非常有益的方面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM