-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclasses.qs
More file actions
105 lines (78 loc) · 1.56 KB
/
Copy pathclasses.qs
File metadata and controls
105 lines (78 loc) · 1.56 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
# QuantoScript Classes - v0.1.0
# Basic object-oriented programming support
# ─── Basic class with constructor ───
class Person {
init(name) {
self.name = name
}
greet() {
print(self.name)
}
}
p = Person("Alice")
p.greet() # Alice
# ─── Multiple objects ───
a = Person("Bob")
b = Person("Charlie")
a.greet() # Bob
b.greet() # Charlie
# ─── Class with multiple fields (must use multi-line bodies) ───
class Point {
init(x) {
self.x = x
self.y = 0
}
}
pt = Point(3)
print(pt.x) # 3
# ─── Object without constructor ───
class Empty {
}
e = Empty()
print("created") # created
# ─── Counter example ───
class Counter {
init() {
self.x = 0
}
inc() {
self.x = self.x + 1
}
get() {
return self.x
}
}
c = Counter()
c.inc()
c.inc()
print(c.x) # 2
# ─── Object references (shared identity) ───
a = Counter()
b = a
b.inc()
print(a.get()) # 1 (shared identity)
# ─── Nested objects ───
class Inner {
init(v) {
self.v = v
}
}
class Outer {
init() {
self.inner = Inner(77)
}
}
o = Outer()
print(o.inner.v) # 77
# ─── Multiple method calls ───
class Math {
add(x, y) {
return x + y
}
double(x) {
return x * 2
}
}
m = Math()
print(m.add(3, 4)) # 7
print(m.double(m.add(1, 2))) # 6