開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- Script言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のVI部(オブジェクト指向プログラミング)24章(クラスのコーディング(詳細))の練習問題6を解いてみる。
その他参考書籍
6.
GenericDisplayクラスの__str__メソッドが動作して、そのなかにあるgatherAttrsメソッド(これもツリー上を検索されてGenericDisplayクラスのメソッドに辿り着く)が動作する。
確認。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- class GenericDisplay: def gather_attrs(self): return "gather_attrs" def __str__(self): return "{0} {1}".format("str", self.gather_attrs()) class Person(GenericDisplay): def __init__(self): print("init") def last_name(self):print("last_name") def birth_day(self): print("birth_day") class Employee(Person): def __init__(self):print("Employee init") def birth_day(self):print("Employee birth_day") def give_raise(self): print("give_raise") if __name__ == '__main__': sue = Employee() print(sue)
入出力結果(Terminal)
$ ./sample.py Employee init str gather_attrs $
ちなみにJavaScriptの場合。
コード(BBEdit)
var GenericDisplay = function(){}; GenericDisplay.prototype.gather_attrs = function(){ return "gather_attrs"; }; GenericDisplay.prototype.toString = function(){ return "str" + " " + this.gather_attrs(); }; var Person = function(){ GenericDisplay.apply(this); }; Person.prototype = new GenericDisplay(); Person.prototype.last_name = function(){ $('#pre0').append("last_name\n"); }; Person.prototype.birth_day = function(){ $('#pre0').append("birth_day\n"); }; var Employee = function(){ Person.apply(this); }; Employee.prototype = new Person(); Employee.prototype.birth_day = function(){ $('#pre0').append("Employee birth_day\n"); }; Employee.prototype.give_raise = function(){ $('#pre0').append("give_raise\n"); }; var sue = new Employee(); $('#pre0').text(sue);
0 コメント:
コメントを投稿