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

Commit c20b92d

Browse files
committed
新增静态成员测试文件,验证静态字段和方法的访问及对象创建功能,进一步增强面向对象编程的能力。
1 parent 0fbccfc commit c20b92d

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

test_static_members.cn

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using lib <io>;
2+
3+
// 静态成员测试类
4+
class MathUtils {
5+
static PI : float = 3.14159;
6+
static counter : int = 0;
7+
8+
static fn add(a : int, b : int) : int {
9+
return a + b;
10+
};
11+
12+
static fn getPI() : float {
13+
return MathUtils::PI;
14+
};
15+
};
16+
17+
class Person {
18+
public name : string;
19+
static totalCount : int = 0;
20+
21+
constructor(name : string) {
22+
this.name = name;
23+
};
24+
25+
public fn getName() : string {
26+
return this.name;
27+
};
28+
};
29+
30+
fn main() : int {
31+
std::println("=== CodeNothing 静态成员测试 ===");
32+
33+
// 测试静态字段访问
34+
std::println("1. 测试静态字段访问");
35+
pi : float = MathUtils::PI;
36+
std::println("PI = " + pi);
37+
std::println("✅ 静态字段访问成功");
38+
39+
// 测试对象创建
40+
std::println("2. 测试对象创建");
41+
person : Person = new Person("Alice");
42+
std::println("Person name: " + person.getName());
43+
std::println("✅ 对象创建成功");
44+
45+
// 测试静态字段访问
46+
std::println("3. 测试多个静态字段");
47+
count : int = Person::totalCount;
48+
std::println("Total count: " + count);
49+
std::println("✅ 静态成员测试成功");
50+
51+
std::println("=== 静态成员测试完成 ===");
52+
return 0;
53+
};

0 commit comments

Comments
 (0)