Skip to content

Commit 90315ba

Browse files
author
Xuefeng
committed
新增示例代表
1 parent 7da2c2b commit 90315ba

File tree

5 files changed

+88
-60
lines changed

5 files changed

+88
-60
lines changed

cmds/2.1.6.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ int main(object me, string arg)
99
printf("s[0..20] = %s\n", s[0..20]);
1010
printf("s[20..30] = %s\n", s[20..30]);
1111
printf("s[1..0] = %s\n", s[1..0]);
12+
printf("s[4..<1] = %s\n", s[4..<1]);
13+
printf("s[<7..<5] = %s\n", s[<7..<5]);
1214

1315
return 1;
1416
}

cmds/5.1.1.c

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,30 @@
1-
private
2-
void test1()
3-
{
4-
printf("%s\n", "A:我是 private test1。");
5-
}
6-
7-
protected
8-
void test2()
9-
{
10-
printf("%s\n", "A:我是 protected test2。");
11-
}
12-
13-
public
14-
void test3()
15-
{
16-
printf("%s\n", "A:我是 public test3。");
17-
}
18-
19-
void test4()
1+
// 示例:5.1.1
2+
// apply 方法,对象加载时自动执行
3+
void create()
204
{
21-
printf("%s\n", "A:我还是 public test4。");
5+
// 发送信息给当前玩家
6+
write("create 5.1.1!\n");
227
}
238

24-
void test()
9+
// apply 方法,设置心跳后自动执行
10+
void heart_beat()
2511
{
26-
printf("%s\n", "A:我是 public 并且 nomask test。");
12+
// 记录日志,请在driver界面或 debug.log 文件中查看
13+
debug_message(file_name(this_object()) + ": " + time());
2714
}
2815

2916
int main(object me, string arg)
3017
{
31-
test1();
32-
test2();
33-
test3();
34-
test4();
35-
test();
36-
return 1;
37-
}
18+
if (query_heart_beat())
19+
{
20+
write("停止心跳!\n");
21+
set_heart_beat(0);
22+
}
23+
else
24+
{
25+
write("开始心跳!\n");
26+
set_heart_beat(1);
27+
}
3828

39-
void create()
40-
{
41-
debug_message("create 5.1.1");
29+
return 1;
4230
}

cmds/5.1.2.c

Lines changed: 0 additions & 28 deletions
This file was deleted.

cmds/5.2.1.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// 示例:5.2.1
2+
3+
// 全局变量,可以在当前文件中使用,不会被存档
4+
nosave string file = "/data/dbase";
5+
// 全局变量,可以在当前文件中使用,会被存档
6+
string data;
7+
8+
int sum(int m, int n);
9+
10+
int main(object me, string arg)
11+
{
12+
// me、arg 是局部变量,可以在 main() 内部使用
13+
if (arg == "load")
14+
{
15+
if (file_size(file + __SAVE_EXTENSION__) > 0)
16+
{
17+
// 读取存档,file 是全局变量
18+
restore_object(file);
19+
printf("data = %s\n", data);
20+
}
21+
else
22+
{
23+
printf("目前没有存档!\n");
24+
}
25+
}
26+
else if (arg)
27+
{
28+
// data 是全局变量
29+
data = arg;
30+
// 数据存档,file 是全局变量
31+
if (!catch (save_object(file)))
32+
{
33+
printf("数据存档成功!\n");
34+
}
35+
else
36+
{
37+
printf("数据存档失败,请确认存档目录 data 存在!\n");
38+
}
39+
}
40+
else
41+
{
42+
// begin、end、result 是块级变量,只能在本代码块内使用
43+
int begin = 1, end = 1024;
44+
int result = sum(begin, end);
45+
printf("The sum from %d to %d is %d\n", begin, end, result);
46+
}
47+
48+
return 1;
49+
}
50+
51+
int sum(int m, int n)
52+
{
53+
int sum = 0;
54+
//m、n、sum 都是局部变量,只能在 sum() 内部使用
55+
// i 是块级变量,只能在 for 循环内部使用
56+
for (int i = m; i <= n; i++)
57+
{
58+
sum += i;
59+
}
60+
return sum;
61+
}

data/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 如果系统未能在 log 目录下自动生成以下二个文件,请手动增加
2+
# author_stats
3+
# domain_stats
4+
*
5+
!.gitignore

0 commit comments

Comments
 (0)