对象系统
对象系统是 Alia 语言的核心数据结构机制,支持灵活的数据组织和操作。
1. 对象定义
基本语法
*[ <Key key>: <Value value>, ... ] ~ 对象定义
*[ static <Key key>: <Value value>, ... ] ~ 静态成员定义
[ <Value value>, ... ] ~ 数组定义
示例
~ 定义对象
const person = *[ name: 'Alice', age: 30 ]
~ 定义数组
const numbers = [1, 2, 3, 4, 5]
~ 访问对象属性
print person.name
print person['age']
print numbers[0]
2. 内置对象类型
| 类型 | 描述 |
|---|---|
| Object String | 字符串 |
| Object Number | 数字 |
| Object Boolean | 布尔值 |
| Object Array | 数组 |
| Object Object | 对象 |
| Object Stack | 栈 |
| Object Alias | 关键字别名 |
| Object Error | 错误 |
3. 对象操作
属性访问
const obj = *[ name: 'Alia', version: 0.1 ]
~ 访问属性
print obj.name ~ 输出 'Alia'
print obj['version'] ~ 输出 0.1
~ 设置属性
obj.name = 'Alia'
obj.version = 1.0
数组操作
const arr: [Number, ...] = [1, 2, 3]
~ 访问元素
print arr[0] ~ 输出 1
~ 修改元素
arr[0] = 10
~ 数组长度
print arr.length ~ 输出 3
4. static 成员与 self
static 成员
static 关键字声明对象的静态成员与静态方法。静态成员与普通成员存放位置不同,不继承 self,可通过对象直接访问,无需实例化。
const counter = *[
static count: 0
static increment: alias -> {
counter.count = counter.count + 1
counter.count
}
name: 'counter'
]
print counter.count ~ 输出 0
print (call counter.increment) ~ 输出 1
print (call counter.increment) ~ 输出 2
print counter.name ~ 输出 'counter'
self 关键字
在对象方法内部,self 指向对象本体,可用于访问对象本体内的其他成员与方法。self 无法访问静态成员;静态方法既无法用 self 访问,也无法访问 self。
const obj = *[
value: 42
getValue: alias -> {
self.value
}
]
print (call obj.getValue) ~ 输出 42
const obj = *[
static staticValue: 100
value: 200
tryAccessStatic: alias -> {
self.staticValue ~ self 无法访问静态成员,结果为空
}
]
print (call obj.tryAccessStatic) ~ 输出 undefined
5. Object 静态方法
Object 类型提供一组静态方法。多参数调用以辅助关键字分隔参数:
const obj = *[ a: 1, b: 2 ]
print Object.keys obj ~ 输出 a,b
print Object.values obj ~ 输出 1,2
print Object.has obj keyed 'a' ~ 输出 true
print Object.get obj keyed 'a' ~ 输出 1
print Object.get obj keyed 'c' or 42 ~ 输出 42
Object.set obj keyed 'c' to 3
print Object.get obj keyed 'c' ~ 输出 3
print Object.delete obj keyed 'c' ~ 输出 true
print Object.isObject obj ~ 输出 true
| 方法 | 参 数 | 说明 |
|---|---|---|
Object.create | value | 创建对象 |
Object.fromEntries | entries | 从键值对数组创建对象 |
Object.assign | target with source... | 合并对象 |
Object.keys | obj | 获取对象键数组 |
Object.values | obj | 获取对象值数组 |
Object.entries | obj | 获取对象键值对数组 |
Object.contact | left with right | 连接两个对象 |
Object.isObject | value | 判断是否为对象 |
Object.clone | obj | 克隆对象 |
Object.has | obj keyed key | 判断对象是否包含指定键 |
Object.set | obj keyed key to value | 设置对象属性 |
Object.get | obj keyed key or fallback | 获取对象属性,带默认值 |
Object.delete | obj keyed key | 删除对象属性 |
const left = *[ x: 1 ]
const right = *[ y: 2 ]
const merged = Object.contact left with right
print merged.x .. merged.y ~ 输出 12
const assigned = Object.assign left with right
print assigned.y ~ 输出 2
6. 示例代码
~ 对象系统综合示例
~ 1. 创建对象
const car = *[
brand: 'Tesla',
model: 'Model 3',
year: 2023,
features: ['autopilot', 'touchscreen', 'electric']
]
~ 2. 访问对象属性
print 'Brand: ' .. car.brand
print 'Model: ' .. car.model
print 'Year: ' .. car.year
~ 3. 遍历数组
print 'Features:'
for { let i = 0 } i < car.features.length { i ++ } {
print ' - ' .. car.features[i]
}
~ 4. 修改属性
car.year = 2024
print 'Updated year: ' .. car.year
~ 5. 添加新属性
Object.set car keyed 'color' to 'white'
print 'Color: ' .. car.color
~ 6. 创建复杂对象
const company = *[
name: 'Tech Corp',
employees: [
*[ name: 'Alice', role: 'Engineer' ],
*[ name: 'Bob', role: 'Manager' ]
],
departments: *[
engineering: 50,
marketing: 20,
sales: 30
]
]
~ 7. 访问嵌套属性
print 'Company: ' .. company.name
print 'Engineers: ' .. company.departments.engineering
print 'First employee: ' .. company.employees[0].name