Julia 배우기(9): 여전히 Julia의 유형에 대해

13602 단어 juliadatatype
알림: 이 게시물은 이전 게시물의 확장 학습 노트입니다.

Julia는 클래스 개념이 없다는 의미인 OOP 언어가 아닙니다. 그러나 Julia에는 상당히 정교한 데이터 유형 계층 구조가 있어 일부 클래스와 같은 작업을 수행할 수 있습니다. 오늘 학습노트의 주제입니다.

오늘 저는 Julia의 데이터 유형에 대한 다음 개념에 익숙해졌습니다.
  • 하위 유형 지정 규칙
  • 추상 유형
  • 프리미티브 유형
  • 복합 유형 및 가변 복합 유형
  • 파라메트릭 추상 유형

  • 기억해야 할 사항:

    Abstract types can be subtyped but not instantiated.
    Concrete types cannot be subtyped.



    다음은 몇 가지 연습을 위한 코드입니다.

    
    using InteractiveUtils 
    println("  ")
    # symbol  <: means "is a subtype of"
    #=
    One particularly distinctive feature of Julia's type system is that concrete types may not subtype each other: 
    all concrete types are final and may only have abstract types as their supertypes.
    =#
    
    #############################
    # abstract type
    #############################
    
    abstract type Cat  end   ## this is how we define an "abstract type"
    println("Type of Cat: ", typeof(Cat)) 
    
    # Cat IS A SUBSET OF Any
    abstract type Cat <: Any end
    println("Supertype of Cat: ", supertype(Cat)) 
    
    # chaton IS A SUBSET OF Cat 
    struct chaton <: Cat
        name::String
    end
    
    # now we define an instance of chaton
    a = chaton("yoyo")  # how its constructor works
    
    println("a: $a, typeof a: ",  typeof(a)) 
    println("  ")
    
    #############################
    # primitive Types
    #############################
    primitive type myReal16 <: Real 16 end  # 16 is bits number of this datatype 
    primitive type myAny <: Any 32 end  # 32 is bits number of this datatype
    
    println("Type of myReal16: ",  typeof(myReal16)) 
    println("Supertype of myReal16: ",  supertype(myReal16)) 
    println("Type of myAny: ",  typeof(myAny)) 
    println("Supertype of myReal16: ",  supertype(myReal16)) 
    
    
    #############################
    # composite Types and mutable composite Types 
    #############################
    println("  ")
    
    abstract type Bird end
    
    ## by default, a composite Type (struct) is immutable
    # Duck IS A SUBSET OF Bird 
    struct Duck <:  Bird
      age::Float64
      color # namely `::Any`
    end
    
    ## we can use keyword mutable to specify a mutable struct
    # mutableDuck IS A SUBSET OF Bird 
    mutable struct mutableDuck <:  Bird
      age::Float64
      color # namely `::Any`
    end
    
    # constructor function by default takes the members of struct
    lolo = Duck(12, "Yellow")
    println("lolo : $lolo ")
    
    ## check if Duck instance is mutable 
    try 
       donald = Duck(35,"orange") 
       donald.age = 45
       println("donald : $donald ")
    catch e
        println(e)
    end
    
    ## check if mutableDuck instance is mutable 
    try 
       donald = mutableDuck(3.5,"orange") 
       donald.age = 45
    catch e
        println(e)
    end
    
    
    # use typeof() to get constructor name
    huahua = typeof(lolo)(6, "rouge") 
    println("huahua : $huahua ")
    
    ### test using abstract Type for constructing an object
    try 
       yoyo = Bird(3.5,"orange") 
    catch e
        println("Error about yoyo: ", e)
    end
    
    
    
    struct Parrot <: Bird # Parrot IS A SUBSET OF Bird 
      age
      color
      sing::String
    end
    
    ### constructor with several members' values fixed
    Parrot(sing::String) = Parrot(13, "green", sing)
    momo = Parrot("kuakuakua")
    println("momo: $momo, Type of momo: ",  typeof(momo)) 
    
    struct Swan <: Bird # Swan IS SUBTYPE OF Bird
      color
      Swan() = new("snowwhite")
      # Swans has only one constructor : Swan()
    end
    popo = Swan()
    
    println("$popo, Type of popo: ",  typeof(popo)) 
    
    println("  ")
    #############################
    # Parametric Abstract Types
    #############################
    
    abstract type Coordinate3D{T} end
    
    
    b = Coordinate3D{Int64} <: Coordinate3D
    println("b : $b")
    
    struct Location3D{T} <: Coordinate3D{T}
               x::T
               y::T
               z::T
           end
    
    pp = Location3D{Int32}(2, 5, 6)
    println("pp : $pp, type:", typeof(pp))
    println("pp.x : $(pp.x), pp.y: $(pp.y), pp.z: $(pp.z) ")
    


    실행 결과:

    좋은 웹페이지 즐겨찾기