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
"""
====================================================================


 

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