Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit a11326a

Browse files
committed
新增基础OOP测试文件,包含Person类的定义及对象创建、字段访问和多个对象测试,验证类的基本功能,进一步增强面向对象编程的支持。
1 parent 504df14 commit a11326a

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

test_oop_basic.cn

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using lib <io>;
2+
3+
// 基础OOP测试 - 类定义和对象创建
4+
class Person {
5+
public name : string;
6+
private age : int;
7+
protected email : string;
8+
9+
constructor(name : string, age : int) {
10+
this.name = name;
11+
this.age = age;
12+
this.email = "";
13+
};
14+
15+
public fn getName() : string {
16+
return this.name;
17+
};
18+
19+
public fn getAge() : int {
20+
return this.age;
21+
};
22+
23+
public fn setEmail(email : string) : void {
24+
this.email = email;
25+
};
26+
27+
public fn introduce() : string {
28+
return "Hello, I am " + this.name;
29+
};
30+
};
31+
32+
fn main() : int {
33+
std::println("=== CodeNothing OOP 基础测试 ===");
34+
35+
// 测试对象创建
36+
std::println("1. 测试对象创建");
37+
person : Person = new Person("Alice", 25);
38+
std::println("✅ 对象创建成功");
39+
40+
// 测试字段访问
41+
std::println("2. 测试字段访问");
42+
std::println("Name: " + person.name);
43+
std::println("✅ 字段访问成功");
44+
45+
// 测试多个对象
46+
std::println("3. 测试多个对象");
47+
person2 : Person = new Person("Bob", 30);
48+
std::println("Person1 name: " + person.name);
49+
std::println("Person2 name: " + person2.name);
50+
std::println("✅ 多对象测试成功");
51+
52+
std::println("=== 基础OOP测试完成 ===");
53+
return 0;
54+
};

0 commit comments

Comments
 (0)