開発環境
- 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-1を解いてみる。
その他参考書籍
練習12-1.
コード
using System;
class Invoice
{
private string vendor;
private int amount;
public Invoice(string vendor, int amount)
{
this.vendor = vendor;
this.amount = amount;
}
public string Vendpor
{
get { return vendor; }
set { vendor = value; }
}
public int Amount
{
get { return amount; }
set { amount = value; }
}
public static Invoice operator +(Invoice lhs, Invoice rhs)
{
if (lhs.vendor == rhs.vendor)
{
return new Invoice(lhs.vendor, lhs.amount + rhs.amount);
}
else
{
return new Invoice("", 0);
}
}
public override string ToString()
{
return vendor + " " + amount;
}
}
class Tester
{
public void Run()
{
Invoice yamato1 = new Invoice("Yamato", 5);
Invoice yamato2 = new Invoice("Yamato", 10);
Invoice sagawa1 = new Invoice("Sagawa", 5);
Invoice sagawa2 = new Invoice("Sagawa", 10);
Invoice[] invoices = { yamato2, sagawa1, sagawa2 };
foreach (Invoice invoice in invoices)
{
Console.WriteLine("{0} + {1} = {2}", yamato1, invoice, yamato1 + invoice);
}
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
入出力結果(Console Window)
Yamato 5 + Yamato 10 = Yamato 15 Yamato 5 + Sagawa 5 = 0 Yamato 5 + Sagawa 10 = 0 続行するには何かキーを押してください . . .
ちなみにJavaScriptの場合。
コード(BBEdit)
function Invoice(vendor, amount){
var vendor = vendor;
var amount = amount;
this.__defineGetter__("vendor", function(){
return vendor});
this.__defineSetter__("vendor", function(val){
vendor = val;});
this.__defineGetter__("amount", function(){
return amount;});
this.__defineSetter__("amount", function(val){
amount = val;});
this.add = function(other){
if(vendor === other.vendor){
return new Invoice(vendor, amount + other.amount);
} else {
return new Invoice("", 0);
}
};
this.toString = function(){
return vendor + " " + amount;
};
}
var yamato1 = new Invoice("Yamato", 5);
var yamato2 = new Invoice("Yamato", 10);
var sagawa1 = new Invoice("Sagawa", 5);
var sagawa2 = new Invoice("Sagawa", 10);
var invoices = [yamato2, sagawa1, sagawa2];
var result = "";
for(var i = 0; i < invoices.length; i++){
result += yamato1.toString() + " + " + invoices[i] + " = " +
yamato1.add(invoices[i]).toString() + "\n";
}
$('#pre0').text(result);
pythonの場合。
sample.py
コード(BBEdit)
#!/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, val):
self._vendor = val
def get_amount(self):
return self._amount
def set_amount(self, val):
self._amount = val
def __add__(self, other):
if self._vendor == other._vendor:
return Invoice(self._vendor, self._amount + other._amount)
else:
return Invoice("", 0)
def __str__(self):
return "{0} {1}".format(self._vendor, self._amount)
if __name__ == '__main__':
yamato1 = Invoice("yamato", 5)
yamato2 = Invoice("yamato", 10)
sagawa1 = Invoice("sagawa", 5)
sagawa2 = Invoice("sagawa", 10)
for x in [yamato2, sagawa1, sagawa2]:
print("{0} + {1} = {2}".format(yamato1, x, yamato1 + x))
入出力結果(Terminal)
$ ./sample.py yamato 5 + yamato 10 = yamato 15 yamato 5 + sagawa 5 = 0 yamato 5 + sagawa 10 = 0 $
0 コメント:
コメントを投稿