Automatically inherit bindings in subclasses of Racket

I have a class with several subclasses that uses methods and fields from the parent class. Is there a β€œright” way to handle this?

So far I have used (inherit method1 method2 ...)in every subclass.

I searched in vain for a way in which a parent class can force subclasses to inherit bindings, and I understand that this may be a bad style.

Not very experienced with Racket or OOP.

+3
source share
1 answer

, inherit. , (send this method arg1 ...). (inherit method) (method arg1 ...) . , , (send this method).

, , . :

(define-syntax (inherit-from-car stx) 
  (datum->syntax stx '(inherit wash buy sell)))

(define car% (class object%
               (define/public (wash) (display "Washing\n"))
               (define/public (buy)  (display "Buying\n"))
               (define/public (sell) (display "Selling\n"))
               (super-new)))

(define audi% (class car% (super-new)
                (inherit-from-car)
                (define/public (wash-and-sell)
                  (wash)
                  (sell))))

(define a-car (new audi%))
(send a-car wash-and-sell)
+2

All Articles