2013年1月11日金曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のVI部(オブジェクト指向プログラミング)のまとめ演習9.(「死んだオウム」スケッチ(芝居のシミュレーション))を解いてみる。

その他参考書籍

9.(「死んだオウム」スケッチ(芝居のシミュレーション))

コード(BBEdit)

sample.py

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

class Actor:
    def line(self):
        self.says()

class Customer(Actor):
    def says(self):
        print("customer: \"that's one ex-bird!\"")

class Clerk(Actor):
    def says(self):
        print("clerk: \"no it isn't...\"")

class Parrot(Actor):
    def says(self):
        print("parrot: None")

class Scene:
    def __init__(self):
        self.customer = Customer()
        self.clerk = Clerk()
        self.parrot = Parrot()
    def action(self):
        actors = [self.customer, self.clerk, self.parrot]
        for actor in actors:
            actor.line()

if __name__ == '__main__':
    Scene().action()

入出力結果(Terminal)

$ ./sample.py
customer: "that's one ex-bird!"
clerk: "no it isn't..."
parrot: None
$

ちなみにJavaScriptの場合。

コード(BBEdit)

function Actor(){
  this.line = function(){
    this.says();
  };
}
function Customer(){
  this.says = function(){
    $('#pre0').append("customer: \"that's one ex-bird!\"\n");
  };
  Actor.apply(this, arguments);
}
Customer.prototype = new Actor();
function Clerk(){
  this.says = function(){
    $('#pre0').append("clerk: \"no it isn't...\"\n");
  };
}
Clerk.prototype = new Actor();
function Parrot(){
  this.says = function(){
    $('#pre0').append("parrot: None\n");
  };
}
Parrot.prototype = new Actor();
function Scene(){
  var customer = new Customer();
  var clerk = new Clerk();
  var parrot = new Parrot();
  var actors = [customer, clerk, parrot];
  this.line = function(){
    for(var i = 0; i < actors.length; i++){
      actors[i].line();
    }
  };
}

new Scene().line();







						

0 コメント:

コメントを投稿