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

Commit a6d98d0

Browse files
committed
新增复杂OOP测试文件,包含Student、Course和University类的定义及对象创建、字段访问和对象集合测试,验证多类交互的功能,进一步增强面向对象编程的支持。
1 parent a11326a commit a6d98d0

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

test_oop_complex.cn

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using lib <io>;
2+
3+
// 复杂OOP测试 - 多类交互
4+
class Student {
5+
public name : string;
6+
public studentId : string;
7+
private grades : string;
8+
9+
constructor(name : string, studentId : string) {
10+
this.name = name;
11+
this.studentId = studentId;
12+
this.grades = "";
13+
};
14+
15+
public fn getName() : string {
16+
return this.name;
17+
};
18+
19+
public fn getStudentId() : string {
20+
return this.studentId;
21+
};
22+
23+
public fn getInfo() : string {
24+
return "Student: " + this.name + " (ID: " + this.studentId + ")";
25+
};
26+
};
27+
28+
class Course {
29+
public courseName : string;
30+
public courseCode : string;
31+
private instructor : string;
32+
33+
constructor(courseName : string, courseCode : string, instructor : string) {
34+
this.courseName = courseName;
35+
this.courseCode = courseCode;
36+
this.instructor = instructor;
37+
};
38+
39+
public fn getCourseName() : string {
40+
return this.courseName;
41+
};
42+
43+
public fn getCourseCode() : string {
44+
return this.courseCode;
45+
};
46+
47+
public fn getInstructor() : string {
48+
return this.instructor;
49+
};
50+
51+
public fn getCourseInfo() : string {
52+
return this.courseCode + ": " + this.courseName;
53+
};
54+
};
55+
56+
class University {
57+
public name : string;
58+
public location : string;
59+
60+
constructor(name : string, location : string) {
61+
this.name = name;
62+
this.location = location;
63+
};
64+
65+
public fn getName() : string {
66+
return this.name;
67+
};
68+
69+
public fn getLocation() : string {
70+
return this.location;
71+
};
72+
73+
public fn getUniversityInfo() : string {
74+
return this.name + " (" + this.location + ")";
75+
};
76+
};
77+
78+
fn main() : int {
79+
std::println("=== CodeNothing OOP 复杂测试 ===");
80+
81+
// 创建大学
82+
std::println("1. 创建大学对象");
83+
university : University = new University("MIT", "Cambridge, MA");
84+
std::println("University: " + university.getUniversityInfo());
85+
std::println("✅ 大学对象创建成功");
86+
87+
// 创建课程
88+
std::println("2. 创建课程对象");
89+
course1 : Course = new Course("Computer Science", "CS101", "Dr. Smith");
90+
course2 : Course = new Course("Mathematics", "MATH201", "Prof. Johnson");
91+
std::println("Course1: " + course1.getCourseInfo());
92+
std::println("Course2: " + course2.getCourseInfo());
93+
std::println("✅ 课程对象创建成功");
94+
95+
// 创建学生
96+
std::println("3. 创建学生对象");
97+
student1 : Student = new Student("Alice Johnson", "S001");
98+
student2 : Student = new Student("Bob Smith", "S002");
99+
student3 : Student = new Student("Charlie Brown", "S003");
100+
std::println("Student1: " + student1.getInfo());
101+
std::println("Student2: " + student2.getInfo());
102+
std::println("Student3: " + student3.getInfo());
103+
std::println("✅ 学生对象创建成功");
104+
105+
// 测试对象集合
106+
std::println("4. 测试对象集合");
107+
std::println("学生1: " + student1.name);
108+
std::println("学生2: " + student2.name);
109+
std::println("学生3: " + student3.name);
110+
std::println("✅ 对象集合测试成功");
111+
112+
// 测试字段访问
113+
std::println("5. 测试字段访问");
114+
std::println("第一个学生姓名: " + student1.name);
115+
std::println("第一个课程名称: " + course1.courseName);
116+
std::println("✅ 字段访问测试成功");
117+
118+
std::println("=== 复杂OOP测试完成 ===");
119+
std::println("🎉 所有OOP功能测试通过!");
120+
return 0;
121+
};

0 commit comments

Comments
 (0)