구조체

Julia 중급

Anthony Markham

Quantitative Developer

구조체 - 소개

  • 구조체는 복합 타입의 한 형태입니다.
  • 원하는 만큼의 필드를 가질 수 있는 타입입니다.
  • 복합 타입으로 그 타입의 객체(인스턴스)를 만들 수 있습니다.
Julia 중급

구조체 - 문법

  • struct 키워드로 구조체를 정의합니다.
  • 각 필드는 struct 선언 내부에 들여쓰기 합니다.
struct Person
    age
    height
    location
end
steve = Person(18, 180, "London")
println(steve.height)
180
Julia 중급

구조체 - 객체의 typeof

  • steve의 타입은 Person입니다.
struct Person
    age
    height
    location
end
steve = Person(18, 180, "London")
println(typeof(steve))
Person
Julia 중급

구조체 - 불변성 시연

  • 구조체는 기본적으로 변경 불가능합니다.
# Steve의 생일!
steve = Person(18, 180, "London")
steve.age = 19
setfield!: immutable struct of type Person cannot be changed
Julia 중급

구조체 - 가변 구조체

  • mutable 키워드로 변경 가능한 구조체를 만듭니다.
mutable struct Person
    age
    height
    location
end
steve = Person(18, 180, "London")
steve.age = 19
println(steve)
Person(19, 180, "London")
Julia 중급

구조체 - 타입 지정 구조체

  • 구조체 정의에서 각 필드의 데이터 타입을 지정할 수 있습니다.
mutable struct Person
    age::Int64
    height::Int64
    location::String
end
steve = Person(18.5, 180, "London")
InexactError: Int64(18.5)
Julia 중급

Ayo berlatih!

Julia 중급

Preparing Video For Download...