julia-類型(第10講)

Julia-類型   2020/6/7
====================================================================
1.類型操作

斷言運算符::
語法:左表達式::右數據類型
用途:判斷“左表達式”是否爲“右數據類型”的實例,true返回“左表達式”值,false返回錯誤

實例:
(9.0*9.0) :: Float64,("A") :: String# (81.0, "A")
(9*9) :: String#TypeError

# ::在local變量使用:代碼塊
語法:變量名::類型 = 值# 變量在賦值同時聲明類型

f_local() :: String=  "Tom"  

function f_local()
    name :: String = "Tom"
    name
    end
    
function f_local() :: String
    name = "Tom"
    name
    end

f_local()# "Tom"
name :: String = "Tom"#global類型-錯誤
local name :: String = "Tom"

====================================================================
2.函數

isa(123,Int64)                                              #測試對象是否具有給定類型:true

primitive type Ptr{T} 64 end
typeof(Float64),typeof(UnionAll),typeof(Ptr)#返回其參數的類型:(DataType, DataType, UnionAll)

supertype(Int64),supertype(Float64),supertype(Any)# 返回類型的父類型:(Signed, AbstractFloat, Any)
subtypes(AbstractFloat)#返回參數的子類型:BigFloat,Float16, Float32,Float64

eltype(["Tom",22])#獲得某個數集的元素類型:Any

====================================================================
3.1.抽象類型:不能實例化,但它們是類型系統的主幹
Int,Float64不是抽象類型,是原始類型。

聲明:
abstract type «name» end                         # 系統默認它的父類型Any
abstract type «name» <: «supertype» end# <: 運算符意義爲「是···的子類型」

用途:抽象類型允許編寫範型函數,調用時用具體類型與之對應

實例:
Unsigned <: Number,Unsigned <: Real      # (true, true)

fx(x,y) = x+y
fx(1,2),fx(1.1,2.2)

====================================================================
3.3.原始類型
抽象類型可以實例化,Julia 允許聲明自己的原始類型,且內置了大量的
原始類型Int8、UInt8、Int16、UInt16、Int32、UInt32、Int64、UInt64、Int128、UInt128、Float16、Float32 和 Float64…………

聲明:
primitive type «name» «bits» end                         # 系統默認它的父類型Any
primitive type «name» <: «supertype» «bits» end# bits 該類型需要存儲空間,name 新類型名稱;
用途:
允許聲明自己原始類型;內置原始類型Int,Float64...

====================================================================
3.4.複合類型
定義:struct  name... end # 複合類型是變量名域的集合

struct Person# 定義複合類型Person
    name::String
    age::Int64
    end

Jack = Person("Jack",27)                        # Person("Jack", 27)
typeof(Jack),Jack.name,Jack.age          # (Person, "Jack", 27) name,age都不允許修改

3.5.可變複合類型
定義:mutable struct  name... end

mutable struct Employee
    name::String
    age::Int64
    end

Boss = Employee("Jack",35)
Boss.name = "Rose"# "Rose"

====================================================================
4.類型共用體
定義:Union{type,...}
說明:類型共同體內定義的類型,比Any少,比原始類型多

實例:
TypeMemo = Union{Char, String}        # 定義類型共同體TypeMemo,只接受字符或字符串
"a" :: TypeMemo, "abc" :: TypeMemo# ("a", "abc")
123 :: TypeMemo# TypeError

====================================================================
5.參數複合類型
定義:struct name{T1,T2,...}
說明:參數複合類型類型可以接受參數,每一個參數值的可能組合引入一個新類型。

實例:
struct Point{T}#定義了參數複合類型Point,在這裏參數有兩個x、y,爲入參類型(泛型)
    x::T
    y::T
    end

# Point作爲一類型,下面每一個Point{T}都是它的子類型
Point{Float64} <: Point,Point{String} <: Point#(true, true)

# 儘管Point入參爲Any類型但其他原始類型並非Point的子類型:
Float64 <: Point# false

# Point下各個子類型相互間也沒有層級關係:
Point{Int64} <: Point{Real}# false

# 參數複合類型Point的實例化和引用格式:
struct Point{T1,T2}
    x::T1
    y::T2
    end

fx(p::Point{<: Float64})=p.x*p.y

function fx(p::Point{<: Float64})
    p.x * p.y
    end

fx(Point(1.1,2.2))# 2.4200000000000004

# 如果複合類型在引用時Point{Real}格式,將不包含Float64類型,所以:
gx(p::Point{Real})=p.x * p.y
gx(Point(1.1,2.2))# MethodError

# 爲了包括包含Float64類型,使用<:運算符
gx(p::Point{<:Real})=p.x * p.y
gx(Point(1.1,2.2))# 2.4200000000000004

# 複合類型Point,可以按照構造函數的形式,定義T1、T2的類型:
Point(1,2),Point(1.2,2.3),Point(1.2,2)
# (Point{Int64,Int64}(1, 2), Point{Float64,Float64}(1.2, 2.3), Point{Float64,Int64}(1.2, 2))

參數複合類型定義可使用mutable關鍵字
====================================================================
6.參數化原始類型
聲明:
primitive type Ptr{T} 64 end#type 關鍵字,T類型參數,64位操作系統
說明:
不同的T表示不同的Ptr類型,儘管都叫Ptr,但是不同類型間無父子關係

primitive type Ptr{T} 64 end
Ptr{Int64} <: Ptr,Ptr{Real} <: Ptr,Ptr{Signed} <: Ptr#(true, true, true)
Ptr{Int64} <: Ptr{Signed},Ptr{Int64} <: Ptr{Real},Ptr{Signed} <: Ptr{Real}#(false, false, false)
# primitive type類型可以理解爲C裏的指針,Ptr{Int64}近似於int*,Ptr{Float64}近似於double*

====================================================================
7.元組類型

數組定義:
data =["Tom",22]
arr = Array{Any,1}(nothing,1);arr[1]="Tom";;push!(arr,22)
arr==data# true

定義元組:
("Tom",22)==Tuple("Tom",22)#可下標訪問;數值不能修改

變參元組類型:
關鍵字Tuple、Vararg,表示該類型元素數量不定。

a1 = Tuple{String,Vararg{Int}}# Tuple{String,Vararg{Int64,N} where N}
isa(("1",),a1),isa(("1",2),a1),isa(("1",2,3),a1),isa(("1",2,3.0),a1)#(true, true, true, false)

命名元組類型:
x = (a=1,b=2.2,c="abc")
x.a==x[1]

====================================================================
8.單態類型

# 簡單地說Type(T)的實例就是T
isa(Float64,Type{Float64}), isa(Float64,Type{Real})#(true, false)

# Type不帶參數時,所有類型對象都是它的實例,當然也包括單態類型:
isa(Float64,Type),isa(String,Type)# (true, true)

====================================================================
9.類型別名
定義:const type

const myint = Int64# Int64
myint                       # Int64 myint是Int64的別名

====================================================================
10.實例:
# 系統內置抽象類型:
function check_all_subtypes(T, space = 0)
    println("\t" ^ space, T)
    for t in subtypes(T)
        if t != Any check_all_subtypes(t, space+1) end
    end
end

check_all_subtypes(Number)
"""
Number
        Complex
        Real
                AbstractFloat
                        BigFloat
                        Float16
                        Float32
                        Float64
                AbstractIrrational
                        Irrational
                Integer
                        Bool
                        Signed
                                BigInt
                                Int128
                                Int16
                                Int32
                                Int64
                                Int8
                        Unsigned
                                UInt128
                                UInt16
                                UInt32
                                UInt64
                                UInt8
                Rational
"""
check_all_subtypes(AbstractString)
"""
AbstractString
        String
        SubString
        SubstitutionString
        Test.GenericString
"""
====================================================================


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章