開発環境
- 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章(オブジェクト指向プログラミング)6.8(練習問題)問題6-2を解いてみる。
その他参考書籍
問題6-2.
コード
using System; class Book { private string title; private string author; private long isbn; public Book(string title, string author, long isbn) { this.title = title; this.author = author; this.isbn = isbn; } public override string ToString() { return "Title:" + title + " Author:" + author + " ISBN:" + isbn; } public void ReadBook() { Console.WriteLine("本を読む"); } public void Shelved() { Console.WriteLine("書棚に保管する"); } } class Tester { public void Run() { Book lcs = new Book("初めてのC#", "Jesse Liberty, Brian MacDonald", 9784873112947); Console.WriteLine(lcs); lcs.ReadBook(); lcs.Shelved(); } static void Main() { Tester t = new Tester(); t.Run(); } }
入出力結果(Console Window)
Title:初めてのC# Author:Jesse Liberty, Brian MacDonald ISBN:9784873112947 本を読む 書棚に保管する 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(BBEdit)
var Book = function(title, author, isbn){ var title = title; var author = author; var isbn = isbn; this.get_title = function(){ return title; }; this.get_auhotr = function(){ return author; }; this.get_isbn = function(){ return isbn; }; }; Book.prototype.toString = function(){ return "Title:" + this.get_title() + " Author:" + this.get_auhotr() + " ISBN:" + this.get_isbn(); }; Book.prototype.read_book = function(){ $('#pre0').append("本を読む\n"); }; Book.prototype.shelved = function(){ $('#pre0').append("書棚に保管する\n"); }; var lcs = new Book("初めてのC#", "Jesse Liberty, Brian MacDonald", 9784873112947);; $('#pre0').append(lcs + "\n"); lcs.read_book(); lcs.shelved();
pythonの場合。
sample.py
コード(BBEdit)
#!/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 get_title(self): return self._title def get_author(self): return self._author def get_isbn(self): return self._isbn def __str__(self): return "Title:{0} Author:{1} ISBN:{2}".format( self._title, self._author, self._isbn) def read_book(self): print("本を読む") def shelved(self): print("書棚に保管する") lcs = Book("初めてのC#", "Jesse Liberty, Brian MacDonald", 9784873112947); print(lcs) lcs.read_book() lcs.shelved()
入出力結果(Terminal)
$ ./sample.py Title:初めてのC# Author:Jesse Liberty, Brian MacDonald ISBN:9784873112947 本を読む 書棚に保管する $
0 コメント:
コメントを投稿