官术网_书友最值得收藏!

Types and collections – inner constructors

Here is another type with only default constructors:

# see the code in Chapter 6\inner_constructors.jl
mutable struct Person
firstname::String
lastname::String
sex::Char
age::Float64
children::Array{String, 1}
end

p1 = Person("Alan", "Bates", 'M', 45.5, ["Jeff", "Stephan"])

This example demonstrates that an object can contain collections, such as arrays or dictionaries. Custom types can also be stored in a collection, just like built-in types, for example:

people = Person[]

This returns 0-element Array{Person,1}:

push!(people, p1)
push!(people, Person("Julia", "Smith", 'F', 27, ["Viral"]))

The show(people) function now returns the following output:

Person[Person("Alan", "Bates", 'M', 45.5, ["Jeff", "Stephan"]), 
Person("Julia", "Smith", 'F', 27.0, ["Viral"])]

Now we can define a function fullname on type Person. You will notice that the definition stays outside the type's code:

fullname(p::Person) = "$(p.firstname) $(p.lastname)"

Or, slightly more performant:

fullname(p::Person) = string(p.firstname, " ", p.lastname)

Now print(fullname(p1)) returns Alan Bates.

If you need to include error checking or transformations as part of the type construction process, you can use inner constructors (so-called because they are defined inside the type itself), as shown in the following example:

mutable struct Family
name::String
members::Array{String, 1}
big::Bool
Family(name::String) = new(name, String[], false)
Family(name::String, members) = new(name, members,
length(members) > 4)
end

We can make a Family object as follows:

fam = Family("Bates-Smith", ["Alan", "Julia", "Jeff", "Stephan", 
"Viral"])

Then the output is as follows:

Family("Bates-Smith",String["Alan","Julia","Jeff","Stephan","Viral"],true)

The keyword new can only be used in an inner constructor to create an object of the enclosing type. The first constructor takes one argument and generates a default for the other two values. The second constructor takes two arguments and infers the value of big. Inner constructors give you more control over how the values of the type can be created. Here, they are written with the short function notation, but if they are multiline, they will use the normal function syntax.

Note that when you use inner constructors, there are no default constructors any more. Outer constructors, calling a limited set of inner constructors, are often the best practice.

主站蜘蛛池模板: 息烽县| 张北县| 凤阳县| 正定县| 北票市| 西丰县| 盐山县| 嘉黎县| 弥勒县| 广丰县| 赤峰市| 日土县| 兴宁市| 内乡县| 南和县| 交口县| 通江县| 和林格尔县| 正阳县| 乌审旗| 舞阳县| 巩留县| 龙川县| 德令哈市| 西充县| 普兰店市| 宜黄县| 章丘市| 咸宁市| 政和县| 庆阳市| 黑河市| 枣阳市| 莱西市| 特克斯县| 阿克苏市| 高清| 临城县| 玉龙| 普定县| 无棣县|