面向对象编程
Sii 语言支持面向对象编程,通过 class 关键字定义类,通过 crob 关键字创建对象,并支持对象的重构机制。
类定义
使用 class 关键字定义类,类名首字母必须大写。
基本语法
class 类名 {
成员变量: 变量类型;
成员变量: 变量类型;
}
示例
class Person {
name: string;
age: int;
email: string;
}
class Calculator {
result: int;
history: arr;
}
类定义限制
- 类只能包含成员变量(属性),不能包含方法
- 类名首字母必须大写
- 成员变量必须指定类型
- 不支持继承、接口等高级面向对象特性
对象创建
使用 crob 关键字创建对象,支持两种方式:简单创建和封装式创建。
简单对象创建
crob 对象名 = new 类名();
示例:
class User {
id: int;
name: string;
age: int;
}
crob user = new User();
user.id = 1;
user.name = "张三";
user.age = 25;
封装式对象创建
crob 对象名 (class: 类名, right: boolean) {
// 字段初始化
字段名 = 值;
// 方法定义
func 方法名(参数列表) {
方法体;
back 返回值;
}
}
封装式对象参数说明
class: 类名:指定要使用的类right: boolean:指定对象是否允许重构true:允许重构,可以使用remake关键字修改对象false:不允许重构
示例:
class BankAccount {
balance: int;
accountNumber: string;
}
crob account (class: BankAccount, right: true) {
balance = 1000;
accountNumber = "123456789";
func deposit(amount: int) {
balance = balance + amount;
back balance;
}
func withdraw(amount: int) {
if (amount <= balance) {
balance = balance - amount;
back true;
} else {
back false;
}
}
func getBalance() {
back balance;
}
}
对象使用
访问成员变量
对象名.成员变量名 = 值;
let 变量名: 类型 = 对象名.成员变量名;
调用方法
对象名.方法名(参数列表);
this关键字
在对象方法中,可以使用this关键字引用当前对象的属性和方法。
语法:
this.属性名
this.方法名(参数列表)
示例:
crob person (class: Person, right: true) {
name = "张三";
age = 25;
func getInfo() {
back "姓名:" + this.name + ",年龄:" + string(this.age);
}
func updateAge(newAge: int) {
this.age = newAge;
back this.getInfo();
}
}
this关键字的作用域:
- 只能在对象方法内部使用
- 引用当前对象的属性和方法
- 当方法参数名与对象属性名相同时,
this.属性名明确指向对象属性
完整示例:
// 使用简单创建的对象
crob user = new User();
user.name = "李四";
user.age = 30;
print(user.name);
// 使用封装式创建的对象
let newBalance: int = account.deposit(500);
let success: bool = account.withdraw(200);
let currentBalance: int = account.getBalance();
// 使用this关键字的示例
crob student (class: Student, right: true) {
id = 1001;
name = "王小明";
grade = 85;
func getInfo() {
back "学生:" + this.name + ",学号:" + string(this.id) + ",成绩:" + string(this.grade);
}
func updateGrade(newGrade: int) {
this.grade = newGrade;
back this.getInfo();
}
对象重构
当对象创建时 right 参数为 true 时,可以使用 remake 关键字重构对象。
重构语法
remake 对象名 {
// 重构现有方法
func 原方法名(参数列表) {
重构的方法体;
back 返回值;
}
// 添加新方法
func 新方法名(参数列表) {
新方法体;
back 返回值;
}
// 添加新字段
新字段名 = 值;
}
重构示例
// 重构现有方法
remake account {
func deposit(amount: int) {
if (amount > 0) {
this.balance = this.balance + amount;
print("存款成功,余额:" + string(this.balance));
back this.balance;
} else {
print("存款金额必须大于0");
back this.balance;
}
}
// 添加新方法
func transfer(targetAccount: obj, amount: int) {
if (amount <= this.balance) {
this.balance = this.balance - amount;
print("转账成功,转出金额:" + string(amount));
back true;
} else {
print("余额不足,转账失败");
back false;
}
}
// 添加新字段
transactionCount = 0;
}
重构注意事项
- 重构限制:只有
right: true的对象才能重构 - 重构时机:重构定义结束后才能调用对象,否则调用的是重构前的对象
- 方法重构:可以重构现有方法或添加新方法
- 字段添加:可以添加新字段,但不能删除现有字段
完整示例
// 定义类
class Student {
id: int;
name: string;
grade: int;
}
// 创建封装式对象
crob student (class: Student, right: true) {
id = 1001;
name = "王小明";
grade = 85;
func getInfo() {
back "学生:" + this.name + ",学号:" + string(this.id) + ",成绩:" + string(this.grade);
}
func updateGrade(newGrade: int) {
this.grade = newGrade;
back this.grade;
}
}
// 使用对象
print(student.getInfo());
student.updateGrade(90);
print(student.getInfo());
// 重构对象
remake student {
func getInfo() {
back "姓名:" + this.name + ",学号:" + string(this.id) + ",当前成绩:" + string(this.grade);
}
func isExcellent() {
back this.grade >= 90;
}
// 添加新字段
attendance = 100;
}
// 使用重构后的对象
print(student.getInfo());
if (student.isExcellent()) {
print("优秀学生!");
}
注意事项
- 类名规范:类名首字母必须大写
- 类型安全:所有成员变量必须指定类型
- 方法绑定:封装式对象的方法通过函数赋值实现
- 重构限制:只有
right: true的对象才能重构 - this关键字:
- 只能在对象方法内部使用
- 引用当前对象的属性和方法
- 当方法参数名与对象属性名相同时,
this.属性名明确指向对象属性 - 编译时会被转换为
对象名.属性名的形式
- 内存管理:对象创建后需要手动管理,没有自动垃圾回收
通过面向对象编程,Sii 语言提供了结构化的代码组织方式,支持数据封装和方法绑定,为复杂应用开发提供了基础。