如何在Julia中定义自己的类型?

huangapple go评论62阅读模式
英文:

How to define our own type in Julia?

问题

Julia> type Circle
ERROR: syntax: extra token "Circle" after end of expression
Stack trace:
1 top-level scope at none:0

我已经尝试了struct方法,但不符合要求。

英文:
Julia> type Circle
	ERROR: syntax: extra token "Circle" after end of expression
Stack trace:
 [1] top-level scope at none:0 

I already tried struct method but it's not working as per requirements.

答案1

得分: 5

你可以使用 struct 声明一个新的(复合)类型:


julia> Circle()
Circle()

还可以查看有关类型的文档

英文:

You can declare a new (composite) type with struct:

julia> struct Circle end

julia> Circle()
Circle()

Also check out the documentation on types.

答案2

得分: 3

以下是翻译好的部分:

"Let's say you want to define your own Circle type and you choose to represent circles by giving their radius and center ((x, y) for simplicity .. 3D coords follow the same pattern of definition). The first thing to decide is whether or not you want to be able to change some value of a Circle once created. Let's assume you want to be able to move its center once created.

  • To create a type that has changeable field values, we use a mutable struct
  • To define a field for our type we give it a name and specify its type
  • To construct a realization of our type, we use a function of the same name
mutable struct Circle
    radius::Float64
    xcoord::Float64
    ycoord::Float64
end

circle1 = Circle(inv(2pi), 0.0, 0.0)
circle2 = Circle(sqrt(2.0), 1.0, 1.0)

然后

julia> circle1 = Circle(inv(2pi), 0.0, 0.0)
Circle(0.15915494309189535, 0.0, 0.0)

julia> circle2 = Circle(sqrt(2.0), 1.0, 1.0)
Circle(1.4142135623730951, 1.0, 1.0)

# and we can move the Circles
julia> circle2.xcoord = 0.0
julia> circle2.ycoord = 0.0
julia> circle2
Circle(1.4142135623730951, 0.0, 0.0)

# and we can change the radius if need be
julia> circle.radius = 1.0
julia> circle2
Circle(1.0, 0.0, 0.0)

Now, we can do a better (neater, clearer) job of it. We may choose to define a 2D Point type (or a 3D Point type), and use that for a field type within Circle. And once we have this type, there are a few embellishments you probably want to incorporate.

diameter(x::Circle) = 2 * x.radius
circumference(x::Circle) = pi * diameter(x)

and you can show it more clearly

Base.show(io::IO, x::Circle) =
   print(io, string("Circle(radius=",x.radius, " x=",x.xcoord, " y=",x.ycoord,")"))

then

julia> circle2
Circle(radius=1.4142135623730951 x=0.0 y=0.0)

For custom types where the field values do not change follow the same pattern replacing mutable struct with struct (omit the mutable). And, of course, there are more advanced techniques that can be applied see in the docs:

Please feel free ask any follow-up questions you may have.

英文:

Let's say you want to define your own Circle type and you choose to represent circles by giving their radius and center ((x, y) for simplicity .. 3D coords follow the same pattern of definition). The first thing to decide is whether or not you want to be able to change some value of a Circle once created. Let's assume you want to be able to move its center once created.

  • To create a type that has changeable field values, we use a mutable struct
  • To define a field for our type we give it a name and specify its type
  • To construct a realization of our type, we use a function of the same name
mutable struct Circle
    radius::Float64
    xcoord::Float64
    ycoord::Float64
end

circle1 = Circle(inv(2pi), 0.0, 0.0)
circle2 = Circle(sqrt(2.0), 1.0, 1.0)

then

julia> circle1 = Circle(inv(2pi), 0.0, 0.0)
Circle(0.15915494309189535, 0.0, 0.0)

julia> circle2 = Circle(sqrt(2.0), 1.0, 1.0)
Circle(1.4142135623730951, 1.0, 1.0)

# and we can move the Circles
julia> circle2.xcoord = 0.0
julia> circle2.ycoord = 0.0
julia> circle2
Circle(1.4142135623730951, 0.0, 0.0)

# and we can change the radius if need be
julia> circle.radius = 1.0
julia> circle2
Circle(1.0, 0.0, 0.0)

Now, we can do a better (neater, clearer) job of it. We may choose to define a 2D Point type (or a 3D Point type), and use that for a field type within Circle. And once we have this type, there are a few embellishments you probably want to incorporate.

diameter(x::Circle) = 2 * x.radius
circumference(x::Circle) = pi * diameter(x)

and you can show it more clearly

Base.show(io::IO, x::Circle) =
   print(io, string("Circle(radius=",x.radius, " x=",x.xcoord, " y=",x.ycoord,")"))

then

julia> circle2
Circle(radius=1.4142135623730951 x=0.0 y=0.0)

For custom types where the field values do not change follow the same pattern replacing mutable struct with struct (omit the mutable). And, of course, there are more advanced techniques that can be applied see in the docs:

Please feel free ask any follow-up questions you may have.

huangapple
  • 本文由 发表于 2020年1月6日 23:25:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定