開発環境
- 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)の 第12章(演算子のオーバーロード)12.6(練習問題)練習12-1を解いてみる。
その他参考書籍
練習12-1.
コード
using System; class Invoice { private string vendor; private double amount; public string Vendor { get { return vendor; } set { vendor = value; } } public double Amount { get { return amount; } set { amount = value; } } public Invoice(string vendor, double amount) { this.vendor = vendor; this.amount = amount; } public static Invoice operator +(Invoice lhs, Invoice rhs) { if (lhs.vendor == rhs.vendor) { return new Invoice(lhs.vendor, lhs.amount + rhs.amount); } return new Invoice("", 0); } } class Tester { public void Run() { Invoice yamato11 = new Invoice("yamato1", 1.2); Invoice yamato12 = new Invoice("yamato1", 2.10); Invoice yamato21 = new Invoice("yamato2", 1.2); Invoice yamato22 = new Invoice("yamato2", 2.1); Invoice[] invoices = { yamato12, yamato21, yamato22 }; foreach (Invoice invoice in invoices) { Console.WriteLine("({0}) + ({1}) = ({2})", yamato11.Vendor + ":" + yamato11.Amount, invoice.Vendor + ":" + invoice.Amount, (yamato11 + invoice).Vendor + ":" + (yamato11 + invoice).Amount); } } static void Main() { Tester t = new Tester(); t.Run(); } }
入出力結果(Console Window)
(yamato1:1.2) + (yamato1:2.1) = (yamato1:3.3) (yamato1:1.2) + (yamato2:1.2) = (:0) (yamato1:1.2) + (yamato2:2.1) = (:0) 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(TextWrangler)
// JavaScriptでは演算子のオーバーロードはできない(?)のでとりあえずaddメソッドを定義してみることに var result = ""; var Invoice = function(vendor, amount){ var vendor = vendor; var amount = amount; this.get_vendor = function(){ return vendor; }; this.set_vendor = function(value){ vendor = value; }; this.get_amount = function(){ return amount; }; this.set_amount = function(value){ amount = value; }; this.add = function(other){ if(vendor === other.get_vendor()){ return new Invoice(vendor, amount + other.get_amount()); } else { return new Invoice("", 0); } }; }; var yamato11 = new Invoice("yamato1", 1.2); var yamato12 = new Invoice("yamato1", 2.1); var yamato21 = new Invoice("yamato2", 1.2); var yamato22 = new Invoice("yamato2", 2.1); var invoices = [yamato12, yamato21, yamato22]; for(var i = 0; i < invoices.length; i++){ result += "(" + yamato11.get_vendor() + ":" + yamato11.get_amount() + ") + " + "(" + invoices[i].get_vendor() + ":" + invoices[i].get_amount() + ") = " + "(" + (yamato11.add(invoices[i])).get_vendor() + ":" + (yamato11.add(invoices[i])).get_amount() + ")\n"; } $('#pre0').text(result);
pythonの場合。
sample.py
コード(TextWrangler)
#!/usr/bin/env python3.3 # -*- coding: utf-8 -*- class Invoice: def __init__(self, vendor, amount): self._vendor = vendor self._amount = amount def get_vendor(self): return self._vendor def set_vendor(self, value): self._vendor = value def get_amount(self): return self._amount def set_amount(self, value): self._amount = amount def __add__(self, other): if self._vendor == other.get_vendor(): return Invoice(self._vendor, self._amount + other.get_amount()) else: return Invoice("", 0) def __radd__(self, other): if self._vendor == other.get_vendor(): return Invoice(self._vendor, other.get_amount() + self._amount) else: return Invoice("", 0) yamato11 = Invoice("yamato1", 1.2) yamato12 = Invoice("yamato1", 2.1) yamato21 = Invoice("yamato2", 1.2) yamato22 = Invoice("yamato2", 2.1) invoices = [yamato12, yamato21, yamato22] for invoice in invoices: print("({0}:{1}) + ({2}:{3}) = ({4}:{5})".format( yamato11.get_vendor(), yamato11.get_amount(), invoice.get_vendor(), invoice.get_amount(), (yamato11 + invoice).get_vendor(), (yamato11 + invoice).get_amount()))
入出力結果(Terminal)
$ ./sample.py (yamato1:1.2) + (yamato1:2.1) = (yamato1:3.3) (yamato1:1.2) + (yamato2:1.2) = (:0) (yamato1:1.2) + (yamato2:2.1) = (:0) $
0 コメント:
コメントを投稿