-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path09-1-basics.rkt
executable file
·105 lines (64 loc) · 2.01 KB
/
09-1-basics.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#lang racket
(require "extras.rkt")
(require rackunit)
;; examples from Lesson 9.1
(define Interface1<%>
(interface ()
foo ; -> Int
bar ; Int -> Int
baz ; Int -> Int
))
(define Class1%
(class* object% (Interface1<%>)
(init-field x y r) ;; x,y,r : Int
(super-new) ;; required magic
;; foo : -> Int
(define/public (foo) (+ x y))
;; bar : Int -> Int
(define/public (bar n) (+ r n))
;; baz : Int -> Int
(define/public (baz n)
(+ (send this foo) n))
))
(define Class2%
(class* object% (Interface1<%>)
(init-field a b c) ; a, b, c : Int
(super-new)
;; foo : -> Int
(define/public (foo) (+ a b))
;; bar : Int -> Int
(define/public (bar n) (* c n))
;; baz : Int -> Int
(define/public (baz n)
(+ (send this foo) n))
))
(define Class2a%
(class* object% (Interface1<%>)
(init-field a b c) ; a, b, c : Int
(field [a1 (- a)]) ; add a new field, initialized to -a
(super-new)
;; foo : -> Int
; (define/public (foo) (+ a b))
(define/public (foo) (- b a1))
;; bar : Int -> Int
(define/public (bar n) (* c n))
;; baz : Int -> Int
(define/public (baz n)
(+ (send this foo) n))
))
(define obj1 (new Class1% [x 10][y 20][r 10]))
(define obj2 (new Class1% [x 15][y 35][r 5]))
(define obj3 (new Class2% [a 15][b 35][c 5]))
(define obj3a (new Class2a% [a 15][b 35][c 5]))
(begin-for-test
(check-equal? (send obj1 foo) 30)
(check-equal? (send obj2 foo) 50)
(check-equal? (send obj1 bar 8) 18)
(check-equal? (send obj2 bar 8) 13)
(check-equal? (send obj1 baz 20) 50)
(check-equal? (send obj2 baz 20) 70)
(check-equal? (send obj2 bar 8) 13)
(check-equal? (send obj3 bar 8) 40)
;; foo is the only method that is different in Class2% and Class2a%.
(check-equal? (send obj3 foo) (send obj3a foo))
)