You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/language/class-object.md
+66-71Lines changed: 66 additions & 71 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,33 +4,29 @@ AIScript offers a powerful class-based object-oriented programming model inspire
4
4
5
5
## Defining a Class
6
6
7
-
Use the `class` keyword to define a class with properties and methods:
7
+
Use the `class` keyword to define a class with properties and methods.
8
8
9
-
```js
9
+
```rust
10
10
classPerson {
11
11
name:str,
12
12
age:int,
13
-
13
+
14
14
fnnew(name:str, age:int) {
15
15
self.name =name;
16
16
self.age =age;
17
17
}
18
-
19
-
fn greet() -> str {
20
-
return"Hello, my name is {self.name} and I'm {self.age} years old.";
21
-
}
22
18
}
23
19
```
24
20
25
-
The `new` method is a special constructor that initializes a new instance of the class.
21
+
The `new()` method is a special constructor that initializes a new instance of the class.
26
22
27
23
## Creating Objects
28
24
29
25
Create a new instance of a class using the class name followed by parentheses containing constructor arguments:
30
26
31
27
```js
32
28
let alice =Person("Alice", 30);
33
-
print(alice.greet()); //"Hello, my name is Alice and I'm 30 years old."
29
+
print(alice); //Person {name: Alice, age: 30}
34
30
```
35
31
36
32
You can also use object literal syntax with type checking:
@@ -41,8 +37,37 @@ let bob = Person {
41
37
age:25,
42
38
};
43
39
44
-
print(bob.greet()); // "Hello, my name is Bob and I'm 25 years old."
40
+
print(bob); // Person {name: Bob, age: 25}
41
+
```
42
+
43
+
## Methods
44
+
45
+
Define methods within a class using the `fn` keyword, declare `self` as the first argument to make the method a instance method, otherwise, it's a static method:
46
+
47
+
```js
48
+
classPerson {
49
+
name: str,
50
+
age: int,
51
+
52
+
fn new(name:str, age:int) {
53
+
self.name= name;
54
+
self.age= age;
55
+
}
56
+
57
+
fn greet(self) -> str {
58
+
print("Hello, my name is", self.name, "and I'm", self.age, "years old.");
59
+
}
60
+
61
+
fn static_method() {
62
+
print("static method of Person");
63
+
}
64
+
}
65
+
66
+
let alice =Person("Alice", 30);
67
+
alice.greet(); // "Hello, my name is Alice and I'm 30 years old."
68
+
Person.static_method(); // "static method of Person"
45
69
```
70
+
Use `class_instance.method()` to call an instance method, use `ClassName.method()` to call a static method.
46
71
47
72
## Accessing Properties and Methods
48
73
@@ -63,46 +88,62 @@ AIScript supports field validation using directive annotations similar to Python
This overview covers the fundamentals of class and object-oriented programming in AIScript. By combining these features with AIScript's error handling, validation, and type system, you can create clean, maintainable, and robust code.
176
+
This overview covers the fundamentals of class and object-oriented programming in AIScript. By combining these features with AIScript's error handling, validation, and type system, you can create clean, maintainable, and robust code.
0 commit comments