version 1.72, 2000/08/15 20:25:07
|
version 1.73, 2000/08/15 20:29:46
|
Line 11366 So first, let's declare methods:
|
Line 11366 So first, let's declare methods:
|
|
|
@example |
@example |
: method ( m v -- m' v ) Create over , swap cell+ swap |
: method ( m v -- m' v ) Create over , swap cell+ swap |
DOES> ( ... o -- ... ) @ over @ + @ execute ; |
DOES> ( ... o -- ... ) @@ over @@ + @@ execute ; |
@end example |
@end example |
|
|
During method declaration, the number of methods and instance |
During method declaration, the number of methods and instance |
Line 11381 Now, we also have to declare instance va
|
Line 11381 Now, we also have to declare instance va
|
|
|
@example |
@example |
: var ( m v size -- m v' ) Create over , + |
: var ( m v size -- m v' ) Create over , + |
DOES> ( o -- addr ) @ + ; |
DOES> ( o -- addr ) @@ + ; |
@end example |
@end example |
|
|
As before, a word is created with the current offset. Instance |
As before, a word is created with the current offset. Instance |
Line 11395 We need a starting point (the base objec
|
Line 11395 We need a starting point (the base objec
|
|
|
@example |
@example |
Create object 1 cells , 2 cells , |
Create object 1 cells , 2 cells , |
: class ( class -- class methods vars ) dup 2@ ; |
: class ( class -- class methods vars ) dup 2@@ ; |
@end example |
@end example |
|
|
For inheritance, the vtable of the parent object has to be |
For inheritance, the vtable of the parent object has to be |
Line 11405 methods of the parent class, which can b
|
Line 11405 methods of the parent class, which can b
|
@example |
@example |
: end-class ( class methods vars -- ) |
: end-class ( class methods vars -- ) |
Create here >r , dup , 2 cells ?DO ['] noop , 1 cells +LOOP |
Create here >r , dup , 2 cells ?DO ['] noop , 1 cells +LOOP |
cell+ dup cell+ r> rot @ 2 cells /string move ; |
cell+ dup cell+ r> rot @@ 2 cells /string move ; |
@end example |
@end example |
|
|
The first line creates the vtable, initialized with |
The first line creates the vtable, initialized with |
Line 11415 copies the xts from the parent vtable.
|
Line 11415 copies the xts from the parent vtable.
|
We still have no way to define new methods, let's do that now: |
We still have no way to define new methods, let's do that now: |
|
|
@example |
@example |
: defines ( xt class -- ) ' >body @ + ! ; |
: defines ( xt class -- ) ' >body @@ + ! ; |
@end example |
@end example |
|
|
To allocate a new object, we need a word, too: |
To allocate a new object, we need a word, too: |
|
|
@example |
@example |
: new ( class -- o ) here over @ allot swap over ! ; |
: new ( class -- o ) here over @@ allot swap over ! ; |
@end example |
@end example |
|
|
Sometimes derived classes want to access the method of the |
Sometimes derived classes want to access the method of the |
Line 11430 first, you could use named words, and se
|
Line 11430 first, you could use named words, and se
|
vtable of the parent object. |
vtable of the parent object. |
|
|
@example |
@example |
: :: ( class "name" -- ) ' >body @ + @ compile, ; |
: :: ( class "name" -- ) ' >body @@ + @@ compile, ; |
@end example |
@end example |
|
|
|
|
Line 11454 Now, implement the two methods, @code{dr
|
Line 11454 Now, implement the two methods, @code{dr
|
|
|
@example |
@example |
:noname ( o -- ) |
:noname ( o -- ) |
>r r@ x @ r@ y @ at-xy r@ text @ r> len @ type ; |
>r r@@ x @@ r@@ y @@ at-xy r@@ text @@ r> len @@ type ; |
button defines draw |
button defines draw |
:noname ( addr u o -- ) |
:noname ( addr u o -- ) |
>r 0 r@ x ! 0 r@ y ! r@ len ! r> text ! ; |
>r 0 r@@ x ! 0 r@@ y ! r@@ len ! r> text ! ; |
button defines init |
button defines init |
@end example |
@end example |
|
|