開発環境
- Microsoft Windows 8 Pro 64bit 日本語 (OS)
- Microsoft Visual Studio Express 2012 for Windows Desktop (IDE)
- プログラミング言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487312-194-7)の 第6章(オブジェクト指向プログラミング)6.8(練習問題)練習6-2.を解いてみる。
その他参考書籍
練習6-2.
コード
using System; class Book { private string title; private string author; private string isbn; public Book(string title, string author, string isbn) { this.title = title; this.author = author; this.isbn = isbn; } public void Read() { Console.WriteLine("本を読む。"); } public void Shelve() { Console.WriteLine("書棚に保管する。"); } public override string ToString() { return "Title: " + title + ", Author:" + author + ", ISBN:" + isbn; } } class Tester { public void Run() { string title = "初めてのC#"; string author = "Jesse Liberty Brian MacDonald"; string isbn = "487311294X"; Book lcs = new Book(title, author, isbn); Console.WriteLine(lcs); lcs.Read(); lcs.Shelve(); } static void Main() { Tester t = new Tester(); t.Run(); } }
入出力結果(Console Window)
Title: 初めてのC#, Author:Jesse Liberty Brian MacDonald, ISBN:487311294X 本を読む。 書棚に保管する。 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(BBEdit)
var Book = function( title, author, isbn ) { var title = title, author = author, isbn = isbn; this.read = function () { $('#pre0').append("本を読む\n"); }, this.shelve = function( ) { $('#pre0').append("書棚に保管する\n"); }, this.toString = function () { return "Title:" + title + ", Author:" + author + ", ISBN:" + isbn; }; }, title = "初めてのJavaScript", author = "Shelley Powers", isbn = "9784873114255", ljs = new Book(title, author, isbn); $('#pre0').append(ljs + "\n"); ljs.read(); ljs.shelve();
pythonの場合。
コード(BBEdit)
sample.py
#!/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(self): print("本を読む。") def shelve(self): print("書棚に保管する。") def __str__(self): return "Title:{0}, Author:{1}, ISBN:{2}".format( self._title, self._author, self._isbn) lpy = Book("初めてのPython", "Mark Lutz", "9784873113937") print(lpy) lpy.read() lpy.shelve()
入出力結果(Terminal)
$ ./sample.py Title:初めてのPython, Author:Mark Lutz, ISBN:9784873113937 本を読む。 書棚に保管する。 $
0 コメント:
コメントを投稿