開発環境
- Microsoft Windows 8 Pro 64bit 日本語 (OS)
- Microsoft Visual Studio Express 2012 for Windows Desktop (IDE)
- プログラミング言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の 第6章(C#のオブジェクト指向プログラミング)6.8(練習問題)練習6-2を解いてみる。
その他参考書籍
問題6-2.
コード
using System; class Book { private string title; private string author; private decimal isbn; public Book(string title, string author, decimal isbn) { this.title = title; this.author = author; this.isbn = isbn; } public void ReadBook() { Console.WriteLine("本を読む。"); } public void Shelve() { Console.WriteLine("書棚に保管する。"); } public override string ToString() { return title + "、" + author + "著、ISBN:" + isbn; } } class Tester { public void Run() { Book lcs = new Book("Learnings C# 3.0", "Jesse Liberty, Brian MacDonald", 9780596521066); Console.WriteLine(lcs); lcs.ReadBook(); lcs.Shelve(); } static void Main() { Tester t = new Tester(); t.Run(); } }
入出力結果(Console Window)
Learnings C# 3.0、Jesse Liberty, Brian MacDonald著、ISBN:9780596521066 本を読む。 書棚に保管する。 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(TextWrangler)
var Book = function(title, author, isbn){ var title = title; var author = author; var isbn = isbn; this.read_book = function(){ return "本を読む。"; }; this.shelve = function(){ return "書棚に保管する。"; }; this.toString = function(){ return title + "、" + author + "著、ISBN:" + isbn; }; }; Book.prototype.read_book = function(){ return "本を読む。"; }; Book.prototype.shelve = function(){ return "書棚に保管する。"; }; var ljs = new Book("Learning JavaScript, 2nd Edition", "Shelley Powers", 9780596521875); var result = ljs.toString() + "\n" + ljs.read_book() + "\n" + ljs.shelve(); $('#pre0').text(result);
pythonの場合。
sample.py
コード(TextWrangler)
#!/usr/bin/env python3.3 #-*- coding utf-8 -*- class Book: def __init__(self, title, author, isbn): self.title = title self.author = author self.isbn = isbn def read_book(self): print("本を読む。") def shelve(self): print("書棚に保管する。") def __str__(self): return "{0}、{1}著、ISBN:{2}".format( self.title, self.author, self.isbn) lpy = Book("Learning Python: Powerful Object-Oriented Programming", "Mark Lutz", 9780596158064) print(lpy) lpy.read_book() lpy.shelve()
入出力結果(Terminal)
$ ./sample.py Learning Python: Powerful Object-Oriented Programming、Mark Lutz著、ISBN:9780596158064 本を読む。 書棚に保管する。 $
0 コメント:
コメントを投稿