2018年4月27日金曜日

開発環境

入門 Python 3 (Bill Lubanovic (著)、斎藤 康毅 (監修)、長尾 高弘 (翻訳)、オライリージャパン)の6章(オブジェクトとクラス)、6.15(復習問題)9、10.を取り組んでみる。

コード(Emacs)

Python 3

#!/usr/bin/env python3

print('6-9')


class Bear:

    def eats(self):
        return 'berries'


class Rabbit:

    def eats(self):
        return 'clover'


class Octotherpe:

    def eats(self):
        return 'campers'

bear = Bear()
rabbit = Rabbit()
octotherpe = Octotherpe()

for o in [bear, rabbit, octotherpe]:
    print(o.eats())

print('6-10')


class Laser:

    def does(self):
        return 'disintegrate'


class Claw:

    def does(self):
        return 'crush'


class SmartPhone:

    def does(self):
        return 'ring'


class Robot:

    def __init__(self):
        self.laser = Laser()
        self.claw = Claw()
        self.smart_phone = SmartPhone()

    def does(self):
        for t in [self.laser, self.claw, self.smart_phone]:
            print(t.does())

robot = Robot()
robot.does()

入出力結果(Terminal, Jupyter(IPython))

$ ./sample3.py
6-9
berries
clover
campers
6-10
disintegrate
crush
ring
$

0 コメント:

コメントを投稿