2013年1月14日月曜日

開発環境

『初めての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 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;
    }
    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 IAppDomainSetup)) return false;
        return this == (Invoice)obj;
    }
    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
}
class Tester
{
    public void Run()
    {
        Invoice yamato = new Invoice("Yamato", 5);
        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 = { yamato1, yamato2, sagawa1, sagawa2 };
        Console.WriteLine("{0}と同じかどうか", yamato);
        foreach (Invoice invoice in invoices)
        {
            string s = yamato == invoice ? "同じ" : "異なる";
            Console.WriteLine("{0}: {1}", invoice, s);
        }
    }
    static void Main()
    {
        Tester t = new Tester();
        t.Run();
    }
}

入出力結果(Console Window)

Yamato 5と同じかどうか
Yamato 5: 同じ
Yamato 10: 異なる
Sagawa 5: 異なる
Sagawa 10: 異なる
続行するには何かキーを押してください . . .

ちなみに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;
  };
  this.equals = function(o){
    if(!(o instanceof Invoice)) return false;
    if(this.vendor === o.vendor && this.amount === o.amount){
      return true;
    }
    return false;
  };
}
var yamato = new Invoice("Yamato", 5);
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 = [yamato1, yamato2, sagawa1, sagawa2];
var result = yamato.toString() + "と等価かどうか\n";
for(var i = 0; i < invoices.length; i++){
  result += invoices[i].toString() + ": ";
  result += invoices[i].equals(yamato) ? "同じ": "異なる";
  result += "\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)
    def __eq__(self, other):
        if type(self) != type(other):
            return false
        if self._vendor == other._vendor and self._amount == other._amount:
            return True
        return False
    def __bool__(self, other):
        return not self == other

if __name__ == '__main__':
    yamato = Invoice("yamato", 5)
    yamato1 = Invoice("yamato", 5)
    yamato2 = Invoice("yamato", 10)
    sagawa1 = Invoice("sagawa", 5)
    sagawa2 = Invoice("sagawa", 10)
    print("{0}と同じかどうか".format(yamato))
    for x in [yamato1, yamato2, sagawa1, sagawa2]:
        result = "同じ" if yamato == x else "異なる"
        print("{0}: {1}".format(x, result))

入出力結果(Terminal)

$ ./sample.py
yamato 5と同じかどうか
yamato 5: 同じ
yamato 10: 異なる
sagawa 5: 異なる
sagawa 10: 異なる
$

0 コメント:

コメントを投稿