開発環境
- 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-2を解いてみる。
その他参考書籍
練習12-2.
コード
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);
}
public static bool operator ==(Invoice lhs, Invoice rhs)
{
if (lhs.vendor == rhs.vendor && lhs.amount == rhs.amount)
{
return true;
}
return false;
}
public static bool operator !=(Invoice lhs, Invoice rhs)
{
return !(lhs == rhs);
}
public override bool Equals(object obj)
{
if (!(obj is Invoice))
{
return false;
}
return this == (Invoice)obj;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
class Tester
{
public void Run()
{
Invoice yamato = new Invoice("yamato1", 1.2);
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 = {yamato11, yamato12, yamato21, yamato22 };
Console.WriteLine("vendor:{0} amount:{1}と同じかどうか?", yamato.Vendor, yamato.Amount);
foreach (Invoice invoice in invoices)
{
string result = yamato == invoice ? "同じ" : "異なる";
Console.WriteLine("vendor:{0} amount:{1} {2}", invoice.Vendor, invoice.Amount, result);
}
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
入出力結果(Console Window)
vendor:yamato1 amount:1.2と同じかどうか? vendor:yamato1 amount:1.2 同じ vendor:yamato1 amount:2.1 異なる vendor:yamato2 amount:1.2 異なる vendor:yamato2 amount:2.1 異なる 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(TextWrangler)
// JavaScriptでは演算子のオーバーロードはできない(?)のでとりあえずequalメソッドを定義してみることに
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);
}
};
this.equals = function(other){
if(typeof(this) != typeof(other))return false;
if(vendor === other.get_vendor() &&
amount === other.get_amount()){
return true;
}
return false;
};
};
var yamato = new Invoice("yamato1", 1.2);
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 = [yamato11, yamato12, yamato21, yamato22];
result += "vneodr:" + yamato.get_vendor() + " amount:" + yamato.get_amount() + "と同じか?\n";
for(var i = 0; i < invoices.length; i++){
result += "name:" + invoices[i].get_vendor() + " amount:" +
invoices[i].get_amount() + " ";
result += yamato.equals(invoices[i]) ? "同じ" : "異なる";
result += "。\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)
def __eq__(self, other):
if self._vendor == other.get_vendor() and self._amount == other.get_amount():
return True
return False
def __ne__(self, other):
return not (self == other)
yamato = Invoice("yamato1", 1.2)
yamato11 = Invoice("yamato1", 1.2)
yamato12 = Invoice("yamato1", 2.1)
yamato21 = Invoice("yamato2", 1.2)
yamato22 = Invoice("yamato2", 2.1)
invoices = [yamato11, yamato12, yamato21, yamato22]
print("vendor:{0} amount:{1}と同じかどうか?".format(yamato.get_vendor(),
yamato.get_amount()))
for invoice in invoices:
result = "同じ" if yamato == invoice else "異なる"
print("vendor:{0} amount:{1} {2}".format(invoice.get_vendor(),
invoice.get_amount(), result))
入出力結果(Terminal)
$ ./sample.py vendor:yamato1 amount:1.2と同じかどうか? vendor:yamato1 amount:1.2 同じ vendor:yamato1 amount:2.1 異なる vendor:yamato2 amount:1.2 異なる vendor:yamato2 amount:2.1 異なる $
0 コメント:
コメントを投稿