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: src/idioms/constructor.md
+205-1Lines changed: 205 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# Constructor
2
2
3
-
Rust không có constructors, thay vào đó có một convention là sử dụng `new` method ([Associated Functions](https://doc.rust-lang.org/stable/book/ch05-03-method-syntax.html#associated-functions)) để tạo một instance mới của struct hoặc enum.
3
+
Rust không có constructors như các ngôn ngữ hướng đối tượng khác (Java, C++, etc.). Thay vào đó, Rust sử dụng convention là tạo [Associated Functions](https://doc.rust-lang.org/stable/book/ch05-03-method-syntax.html#associated-functions) có tên `new` để khởi tạo instance mới.
4
+
5
+
## Convention cơ bản: `new()`
4
6
5
7
```rust
6
8
structPoint {
@@ -20,4 +22,206 @@ fn main() {
20
22
}
21
23
```
22
24
25
+
## Tại sao không dùng `pub` cho fields?
26
+
27
+
Trong thực tế, chúng ta thường giữ các fields là private và cung cấp constructor `new()` để kiểm soát việc khởi tạo:
0 commit comments