CLOS(2)

亀のように進みます。継承してみます。

class Car
def accele
print("アクセルを踏みました\n")
end
def brake
print("ブレーキを踏みました\n")
end
end
class Soarer < Car
def openRoof
print("ルーフを開けました\n")
end
end
class Crown < Car
def reclining
print("シートをリクライニングしました\n")
end
end
soarer = Soarer.new
soarer.openRoof
soarer.accele
crown = Crown.new
crown.reclining
crown.brake
http://www.rubylife.jp/class/inherit/index1.html
CLOSに翻訳
(defclass AutoMobile () ())
(defmethod accele ((car AutoMobile))
(format t "~a  アクセルを踏みました~%" car))
(defmethod brake ((car AutoMobile))
(format t "~a  ブレーキを踏みました~%" car))
(defclass Soarer (AutoMobile) ())
(defmethod openRoof ((soarer Soarer))
(format t "~a  ルーフを開けました~%" soarer))
(defclass Crown (AutoMobile) ())
(defmethod reclining ((crown Crown))
(format t "~a  シートをリクライニングしました~%" crown))
(setf soarer (make-instance 'Soarer))
(openRoof soarer)
(accele soarer)
(setf crown (make-instance 'Crown))
(reclining crown)
(brake crown)
実行結果
#<SOARER #x101FF065>  ルーフを開けました
#<SOARER #x101FF065>  アクセルを踏みました
#<CROWN #x10208F51>  シートをリクライニングしました
#<CROWN #x10208F51>  ブレーキを踏みました

感想

On Lispにも書いてあった気がしますが、他の言語と違って(OOP用の構文があるわけでなく)、CLOSはlispの枠組みの中で、OOPの概念を取り入れただけなので、OOPの概念を純粋に考えるのには、向いているのかもしれないと感じました。

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です


The reCAPTCHA verification period has expired. Please reload the page.

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください