thp-web/md/learn/classes/static.md

58 lines
516 B
Markdown
Raw Normal View History

2023-10-02 01:41:38 +00:00
# Static in classes
## Class constants
```thp
2023-10-24 01:39:34 +00:00
class Cat
{
// Stateful code
}
static Cat
{
2023-10-02 01:41:38 +00:00
const CONSTANT = "constant value"
}
print(Cat::CONSTANT)
```
## Static methods
aka. plain, old functions
```thp
2023-10-24 01:39:34 +00:00
static Cat
{
fun static_method() -> Int
{
2023-10-02 01:41:38 +00:00
// ...
}
}
Cat::static_method()
```
## Static properties
aka. global variables
```thp
2023-10-24 01:39:34 +00:00
static Cat
{
pub var access_count = 0
2023-10-02 01:41:38 +00:00
}
print(Cat::access_count) // 0
Cat::access_count += 1
print(Cat::access_count) // 1
```