2023-10-02 01:41:38 +00:00
|
|
|
# Classes
|
|
|
|
|
|
|
|
Basically kotlin syntax.
|
|
|
|
|
|
|
|
|
|
|
|
## Create a new instance of a class
|
|
|
|
|
|
|
|
`new` not required, in fact, forbidden.
|
|
|
|
|
|
|
|
```thp
|
2023-12-17 01:33:55 +00:00
|
|
|
let dog = Dog()
|
2023-10-02 01:41:38 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Simple class
|
|
|
|
|
|
|
|
Why'd you do this tho?
|
|
|
|
|
|
|
|
```thp
|
|
|
|
class SimpleClass
|
|
|
|
|
2023-12-17 01:33:55 +00:00
|
|
|
let instance = SimpleClass()
|
2023-10-02 01:41:38 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Properties & methods
|
|
|
|
|
|
|
|
```thp
|
|
|
|
class SimpleClass
|
|
|
|
{
|
|
|
|
// Properties are private by default
|
2023-12-17 01:33:55 +00:00
|
|
|
mut String? name = ...
|
2023-10-02 01:41:38 +00:00
|
|
|
|
2023-10-24 01:39:34 +00:00
|
|
|
// Made public with `pub`
|
2023-12-17 01:33:55 +00:00
|
|
|
pub mut String? surname = ...
|
2023-10-02 01:41:38 +00:00
|
|
|
|
|
|
|
// Methods are private by default
|
2023-11-04 12:17:32 +00:00
|
|
|
fun display_name()
|
2023-10-02 01:41:38 +00:00
|
|
|
{
|
2023-12-17 01:33:55 +00:00
|
|
|
// `$` is used instead of $this. Mandatory
|
2023-10-02 01:41:38 +00:00
|
|
|
print($name)
|
|
|
|
}
|
|
|
|
|
2023-11-04 12:17:32 +00:00
|
|
|
pub fun get_name() -> String?
|
2023-10-02 01:41:38 +00:00
|
|
|
{
|
|
|
|
$name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Static members
|
|
|
|
|
|
|
|
no
|
|
|
|
|
|
|
|
|
|
|
|
## Constructor
|
|
|
|
|
|
|
|
Kotlin style
|
|
|
|
|
|
|
|
|
|
|
|
```thp
|
2023-12-17 01:33:55 +00:00
|
|
|
class Cat(
|
|
|
|
// If a parameter has pub, protected or private they are promoted to properties
|
|
|
|
private String name,
|
|
|
|
pub mut Int lives = 9,
|
|
|
|
protected String surname = "Doe",
|
|
|
|
)
|
2023-10-02 01:41:38 +00:00
|
|
|
{
|
2023-11-04 12:17:32 +00:00
|
|
|
pub fun get_name() -> String
|
2023-10-02 01:41:38 +00:00
|
|
|
{
|
|
|
|
$name
|
|
|
|
}
|
2023-12-17 01:33:55 +00:00
|
|
|
|
|
|
|
pub fun die()
|
|
|
|
{
|
|
|
|
$lives -= 1
|
|
|
|
if $lives <= 0
|
|
|
|
{
|
|
|
|
print("Cat {$name} is death")
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print("Cat {$name} is life still")
|
|
|
|
}
|
|
|
|
}
|
2023-10-02 01:41:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val michifu = Cat("Michifu")
|
|
|
|
print(michifu.get_name())
|
|
|
|
```
|
|
|
|
|
2023-11-04 12:17:32 +00:00
|
|
|
With kotlin's `init` block.
|
|
|
|
|
|
|
|
```thp
|
2023-12-17 01:33:55 +00:00
|
|
|
class Dog(pub String name)
|
2023-11-04 12:17:32 +00:00
|
|
|
{
|
2023-12-17 01:33:55 +00:00
|
|
|
Int name_length
|
2023-11-04 12:17:32 +00:00
|
|
|
|
|
|
|
init
|
|
|
|
{
|
|
|
|
print("Dog has been instantiated")
|
|
|
|
$name_length = name.length()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-10-02 01:41:38 +00:00
|
|
|
## Inheritance
|
|
|
|
|
|
|
|
Kotlin style
|
|
|
|
|
|
|
|
```thp
|
2023-12-17 01:33:55 +00:00
|
|
|
class Animal(pub String name)
|
2023-10-02 01:41:38 +00:00
|
|
|
{
|
2023-11-04 12:17:32 +00:00
|
|
|
pub fun say_name()
|
2023-10-02 01:41:38 +00:00
|
|
|
{
|
|
|
|
print($name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Cat(String name, Int lives) -> Animal(name)
|
|
|
|
|
|
|
|
Cat("Michi", 9).say_name()
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|