抽象类型 #
一、抽象类型基础 #
1.1 定义抽象类型 #
julia
abstract type Animal end
abstract type Mammal <: Animal end
abstract type Bird <: Animal end
1.2 类型层次 #
julia
Mammal <: Animal
Bird <: Animal
Animal <: Any
1.3 不能实例化 #
julia
Animal()
二、类型层次设计 #
2.1 数值类型层次 #
julia
abstract type Number end
abstract type Real <: Number end
abstract type AbstractFloat <: Real end
abstract type Integer <: Real end
abstract type Signed <: Integer end
abstract type Unsigned <: Integer end
2.2 集合类型层次 #
julia
abstract type AbstractCollection end
abstract type AbstractSet <: AbstractCollection end
abstract type AbstractDict <: AbstractCollection end
abstract type AbstractVector <: AbstractCollection end
2.3 自定义类型层次 #
julia
abstract type Shape end
abstract type Polygon <: Shape end
abstract type Circle <: Shape end
abstract type Triangle <: Polygon end
abstract type Rectangle <: Polygon end
三、使用抽象类型 #
3.1 函数参数类型 #
julia
function area(s::Shape)
error("area not implemented for $(typeof(s))")
end
struct Circle
radius::Float64
end
area(c::Circle) = π * c.radius^2
struct Rectangle
width::Float64
height::Float64
end
area(r::Rectangle) = r.width * r.height
area(Circle(1.0))
area(Rectangle(3.0, 4.0))
3.2 多态函数 #
julia
function describe(x)
if x isa Integer
"Integer: $x"
elseif x isa AbstractFloat
"Float: $x"
else
"Other: $x"
end
end
describe(1)
describe(1.0)
describe("hello")
3.3 类型约束 #
julia
function process(x::Real)
x * 2
end
process(1)
process(1.0)
process(1 // 2)
process("hello")
四、抽象类型与具体类型 #
4.1 区别 #
julia
isabstracttype(Real)
isabstracttype(Int)
isconcretetype(Int)
isconcretetype(Real)
4.2 类型检查 #
julia
1 isa Real
1.0 isa Real
1 isa Integer
1.0 isa Integer
4.3 子类型关系 #
julia
Int <: Integer
Int <: Real
Int <: Number
Float64 <: AbstractFloat
Float64 <: Real
五、实践练习 #
5.1 练习1:形状层次 #
julia
abstract type Shape end
abstract type Polygon <: Shape end
struct Circle <: Shape
radius::Float64
end
struct Triangle <: Polygon
a::Float64
b::Float64
c::Float64
end
struct Rectangle <: Polygon
width::Float64
height::Float64
end
area(c::Circle) = π * c.radius^2
area(r::Rectangle) = r.width * r.height
area(t::Triangle) = begin
s = (t.a + t.b + t.c) / 2
sqrt(s * (s - t.a) * (s - t.b) * (s - t.c))
end
perimeter(c::Circle) = 2π * c.radius
perimeter(r::Rectangle) = 2 * (r.width + r.height)
perimeter(t::Triangle) = t.a + t.b + t.c
shapes = [Circle(1), Rectangle(3, 4), Triangle(3, 4, 5)]
[area(s) for s in shapes]
5.2 练习2:动物层次 #
julia
abstract type Animal end
abstract type Mammal <: Animal end
abstract type Bird <: Animal end
struct Dog <: Mammal
name::String
end
struct Cat <: Mammal
name::String
end
struct Eagle <: Bird
name::String
end
speak(d::Dog) = "$(d.name) says Woof!"
speak(c::Cat) = "$(c.name) says Meow!"
speak(e::Eagle) = "$(e.name) says Screech!"
animals = [Dog("Buddy"), Cat("Whiskers"), Eagle("Sky")]
[speak(a) for a in animals]
5.3 练习3:数值处理 #
julia
function process_number(x::Number)
if x isa Integer
x^2
elseif x isa AbstractFloat
round(x, digits=2)
elseif x isa Rational
float(x)
elseif x isa Complex
abs(x)
else
x
end
end
process_number(5)
process_number(3.14159)
process_number(1 // 3)
process_number(3 + 4im)
六、总结 #
本章我们学习了:
- 抽象类型定义:abstract type语法
- 类型层次:构建类型树
- 多态函数:基于抽象类型的函数
- 类型检查:isabstracttype、isconcretetype
- 子类型关系:<:运算符
接下来让我们学习Julia的参数化类型!
最后更新:2026-03-27