OO example
This commit is contained in:
parent
09e6557f6d
commit
08f3bf3cbb
|
@ -86,7 +86,26 @@ You can call a proc on objects with the ```anObject.foobar()```, but you can do
|
||||||
|
|
||||||
Nim does not have an implicit _self_.
|
Nim does not have an implicit _self_.
|
||||||
|
|
||||||
It is possible to implement object-orientation features from other languages (like C++,Java,etc. or Smalltalk,Obj-C,Ruby,etc.) through libraries, thanks to the extensive meta-programming features of Nim. These are at the moment mostly work-in-progress.
|
It is possible to implement object-orientation features through libraries, thanks to the extensive meta-programming features of Nim. These are at the moment mostly work-in-progress.
|
||||||
|
|
||||||
|
Object orientation example:
|
||||||
|
|
||||||
|
```Nim
|
||||||
|
type Animal = ref object of RootObj
|
||||||
|
age: int
|
||||||
|
name: string
|
||||||
|
|
||||||
|
type Cat = ref object of Animal
|
||||||
|
playfulness: float
|
||||||
|
|
||||||
|
# A proc that can access and *modify* the object
|
||||||
|
proc increase_age(self: var Cat) =
|
||||||
|
self.age.inc()
|
||||||
|
|
||||||
|
var c = Cat(name: "Tom")
|
||||||
|
c.increase_age()
|
||||||
|
echo c.name, " ", c.age
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Inheritance
|
### Inheritance
|
||||||
|
|
Loading…
Reference in New Issue