開発環境
- 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)の 第12章(演算子のオーバーロード)12.6(練習問題)練習12-2.を解いてみる。
その他参考書籍
練習12-2.
コード
using System; class Invoice { private string vendor; private double amount; public Invoice(string vendor, double amount) { this.vendor = vendor; this.amount = amount; } public string Vendor { get { return vendor; } } public double Amount { get { return 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 i1 = new Invoice("Invoice1", 5); Invoice i2 = new Invoice("Invoice1", 10); Invoice i3 = new Invoice("Invoice2", 5); Invoice i4 = new Invoice("Invoice2", 10); Invoice i5 = new Invoice("Invoice3", 5); Invoice i6 = new Invoice("Invoice3", 10); Invoice[] invoices = {i2, i3, i4, i5, i6 }; foreach (Invoice invoice in invoices) { Invoice i = i1+invoice; Console.WriteLine("({0}, {1}) + ({2}, {3}) = ({4}, {5})", i1.Vendor, i1.Amount, invoice.Vendor, invoice.Amount, i.Vendor, i.Amount); } } static void Main() { Tester t = new Tester(); t.Run(); } }
入出力結果(Console Window)
(Invoice1, 5) == (Invoice1, 10): False (Invoice1, 5) == (Invoice2, 5): False (Invoice1, 5) == (Invoice2, 10): False (Invoice1, 5) == (Invoice1, 5): True 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(BBEdit)
var Invoice = function ( vendor, amount ) { var vendor = vendor, amount = amount; this.getVendor = function ( ) { return vendor; }, this.getAmount = function( ) { return amount; }; }, i1 = new Invoice("Invoice1", 5), i2 = new Invoice("Invoice1", 10), i3 = new Invoice("Invoice2", 5), i4 = new Invoice("Invoice2", 10), i5 = new Invoice("Invoice1", 5), invoices = [i2, i3, i4, i5], result = "", i, max, invoice; Invoice.prototype.add = function( a ) { if (this.getVendor() === a.getVendor()) { return new Invoice(this.getVendor(), this.getAmount() + a.getAmount()); } return new Invoice("", 0); }, Invoice.prototype.isEqual = function (o) { if (((typeof o === typeof Invoice) && (o instanceof Invoice) )) { return false; } return (this.getVendor() === o.getVendor()) && (this.getAmount() === o.getAmount()); }; for (i = 0, max = invoices.length; i < max; i += 1) { result += "(" + i1.getVendor() + ", " + i1.getAmount() + "), (" + invoices[i].getVendor() + ", " + invoices[i].getAmount() + "): " + invoices[i].isEqual(i1) + "\n"; } $('#pre0').text(result);
pythonの場合。
コード(BBEdit)
sample.py
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- class Invoice: def __init__(self, vendor, amount): self._vendor = vendor self._amount = amount def getVendor(self): return self._vendor def getAmount(self): return self._amount def __add__(self, y): if self._vendor == y._vendor: return Invoice(self._vendor, self._amount + y._amount) return Invoice("", 0) def __radd__(self, x): if x._vendor == self._vendor: return Invoice(x._vendor, x._amount + self._amount) return Invoice("", 0) def __eq__(self, y): if self._vendor == y._vendor and self._amount == y._amount: return True return False def __ne__(self, y): return not self == y i1 = Invoice("Invoice1", 5) i2 = Invoice("Invoice1", 10) i3 = Invoice("Invoice2", 5) i4 = Invoice("Invoice2", 10) i5 = Invoice("Invoice1", 5) invoices = [i2, i3, i4, i5] for x in invoices: invoice = i1 + x print("({0}, {1}) == ({2}, {3}): {4}".format( i1.getVendor(), i1.getAmount(), x.getVendor(), x.getAmount(), i1 == x))
入出力結果(Terminal)
$ ./sample.py (Invoice1, 5) == (Invoice1, 10): False (Invoice1, 5) == (Invoice2, 5): False (Invoice1, 5) == (Invoice2, 10): False (Invoice1, 5) == (Invoice1, 5): True $
0 コメント:
コメントを投稿