Repository navigation
Expand file tree
/
Copy path04_classes.candle
More file actions
441 lines (360 loc) · 8.8 KB
/
Copy path04_classes.candle
File metadata and controls
441 lines (360 loc) · 8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/// Candle 语言类和继承模板
/// 展示类定义、继承、接口、扩展等面向对象特性
// ==================== 基础类定义 ====================
/// 简单类定义
class Point {
int x;
int y;
/// 构造函数
Point(int x, int y) {
this.x = x;
this.y = y;
}
/// 实例方法
void display() {
print("Point(");
print(this.x);
print(", ");
print(this.y);
print(")\n");
}
/// 计算距离
double distanceTo(Point other) {
int dx = this.x - other.x;
int dy = this.y - other.y;
return Math.sqrt((dx * dx + dy * dy));
}
}
// ==================== 静态成员 ====================
class Counter {
/// 静态字段(类级别共享)
static int count = 0;
int id;
Counter() {
Counter.count = Counter.count + 1;
this.id = Counter.count;
}
/// 静态方法
static int getCount() {
return Counter.count;
}
void displayId() {
print("Counter ID: ");
print(this.id);
print("\n");
}
}
// ==================== Final 不可变字段 ====================
class Config {
/// final 字段必须在构造时初始化,之后不能修改
final String appName;
final int version;
Config(String appName, int version) {
this.appName = appName;
this.version = version;
}
}
// ==================== 单继承 ====================
/// 父类
class Animal inherit Object {
String name;
int age;
Animal(String name, int age) {
this.name = name;
this.age = age;
}
void eat() {
print(this.name);
print(" is eating\n");
}
void speak() {
print("Animal speaks\n");
}
}
/// 子类继承父类
class Dog inherit Animal {
String breed;
/// 构造函数调用父类构造函数
Dog(String name, int age, String breed) : super(name, age) {
this.breed = breed;
}
/// 重写父类方法
void speak() {
print(this.name);
print(" says: Woof!\n");
}
/// 调用父类方法
void parentSpeak() {
super.speak();
}
/// 子类特有方法
void wagTail() {
print(this.name);
print(" is wagging tail\n");
}
}
/// 多层继承
class Puppy inherit Dog {
Puppy(String name, String breed) : super(name, 1, breed) {
}
void speak() {
print(this.name);
print(" says: Yip yip!\n");
}
}
// ==================== 接口定义 ====================
/// 接口定义抽象行为
interface Drawable {
void draw();
void move(int dx, int dy);
}
interface Clickable {
void onClick();
}
// ==================== 实现接口 ====================
/// 类可以实现一个或多个接口
class Button implements Drawable, Clickable {
int x;
int y;
String label;
Button(int x, int y, String label) {
this.x = x;
this.y = y;
this.label = label;
}
/// 实现 Drawable 接口
void draw() {
print("Drawing button '");
print(this.label);
print("' at (");
print(this.x);
print(", ");
print(this.y);
print(")\n");
}
void move(int dx, int dy) {
this.x = this.x + dx;
this.y = this.y + dy;
}
/// 实现 Clickable 接口
void onClick() {
print("Button '");
print(this.label);
print("' clicked\n");
}
}
// ==================== 抽象类(通过接口模拟)====================
/// Candle 可能不支持 abstract 关键字,使用接口代替
interface Shape {
double area();
double perimeter();
}
class Circle implements Shape {
double radius;
Circle(double radius) {
this.radius = radius;
}
double area() {
return Math.PI * this.radius * this.radius;
}
double perimeter() {
return 2.0 * Math.PI * this.radius;
}
}
class Rectangle implements Shape {
double width;
double height;
Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
double area() {
return this.width * this.height;
}
double perimeter() {
return 2.0 * (this.width + this.height);
}
}
// ==================== 类型扩展 ====================
/// 扩展现有类型添加新方法
extension int {
/// 判断是否为偶数
bool isEven() {
return this % 2 == 0;
}
/// 判断是否为奇数
bool isOdd() {
return this % 2 != 0;
}
/// 计算阶乘
int factorial() {
if (this <= 1) {
return 1;
}
int result = 1;
for (int i = 2; i <= this; i = i + 1) {
result = result * i;
}
return result;
}
}
extension double {
/// 保留指定小数位
double round(int decimals) {
double multiplier = Math.pow(10.0, decimals);
return Math.round(this * multiplier) / multiplier;
}
/// 绝对值
double abs() {
return Math.abs(this);
}
}
extension String {
/// 转大写
String toUpper() {
// 实际实现需要运行时支持
return this;
}
/// 重复字符串
String repeat(int count) {
String result = "";
for (int i = 0; i < count; i = i + 1) {
result = result + this;
}
return result;
}
}
// ==================== 访问控制 ====================
class BankAccount {
/// 公开字段
public String accountNumber;
/// 私有字段(外部不可访问)
private double balance;
BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
/// 公开方法
public double getBalance() {
return this.balance;
}
/// 私有方法(外部不可调用)
private bool validateAmount(double amount) {
return amount > 0.0;
}
public void deposit(double amount) {
if (this.validateAmount(amount)) {
this.balance = this.balance + amount;
}
}
public void withdraw(double amount) {
if (this.validateAmount(amount) && this.balance >= amount) {
this.balance = this.balance - amount;
}
}
}
// ==================== 泛型类 ====================
/// 泛型类可以处理多种类型
class Box<T> {
T value;
Box(T value) {
this.value = value;
}
T getValue() {
return this.value;
}
void setValue(T newValue) {
this.value = newValue;
}
}
// ==================== 综合示例 ====================
int main() {
print("=== Candle 类和继承演示 ===\n\n");
// 基础类
print("1. 基础类:\n");
Point p1 = Point(0, 0);
Point p2 = Point(3, 4);
p1.display();
p2.display();
print("\n");
// 静态成员
print("2. 静态成员:\n");
Counter c1 = Counter();
Counter c2 = Counter();
Counter c3 = Counter();
c1.displayId();
c2.displayId();
c3.displayId();
print("Total count: ");
print(Counter.getCount());
print("\n\n");
// 继承
print("3. 继承:\n");
Dog dog = Dog("Buddy", 3, "Golden Retriever");
dog.eat();
dog.speak();
dog.parentSpeak();
dog.wagTail();
print("\n");
// 多层继承
Puppy puppy = Puppy("Max", "Beagle");
puppy.speak();
print("\n");
// 接口实现
print("4. 接口实现:\n");
Button btn = Button(100, 200, "Submit");
btn.draw();
btn.onClick();
btn.move(10, 20);
btn.draw();
print("\n");
// 多态
print("5. 多态:\n");
Circle circle = Circle(5.0);
Rectangle rect = Rectangle(4.0, 6.0);
print("Circle area: ");
print(circle.area());
print("\n");
print("Rectangle area: ");
print(rect.area());
print("\n\n");
// 扩展方法
print("6. 扩展方法:\n");
if (4.isEven()) {
print("4 is even\n");
}
if (5.isOdd()) {
print("5 is odd\n");
}
print("5! = ");
print(5.factorial());
print("\n");
String repeated = "Hi".repeat(3);
print(repeated);
print("\n\n");
// 泛型类
print("7. 泛型类:\n");
Box<int> intBox = Box<int>(42);
print("Int box: ");
print(intBox.getValue());
print("\n");
Box<String> stringBox = Box<String>("Hello");
print("String box: ");
print(stringBox.getValue());
print("\n\n");
// 访问控制
print("8. 访问控制:\n");
BankAccount account = BankAccount("123456", 1000.0);
print("Account: ");
print(account.accountNumber);
print("\n");
print("Balance: ");
print(account.getBalance());
print("\n");
account.deposit(500.0);
print("After deposit: ");
print(account.getBalance());
print("\n");
print("\n=== 演示完成 ===\n");
return 0;
}