プログラミング(Python、Perl、C、Go、JavaScript)、数学、読書…
開発環境
"初めてのC# 第2版"の第12章(演算子のオーバーロード)の12.6(練習問題)を解いてみる。
練習 12-1
using System; public class Invoice { private string vendor; private double amount; public string Vender { 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); } else { return new Invoice("", 0); } } } class Tester { public void Run() { Invoice firstInvoice = new Invoice("Vendor1", 1.2); Invoice secondInvoice = new Invoice("Vendor1", 3.4); Invoice thirdInvoice = new Invoice("Vendor2", 5.6); Invoice i1 = firstInvoice + secondInvoice; Invoice i2 = firstInvoice + thirdInvoice; Console.WriteLine( "firstInvoice vendor: {0} amount: {1}" + "\nfsecondInvoice vendor: {2} amount: {3}" + "\nthirdInvoice vendor: {4} amount: {5}" + "\nfirstInvoice+fsecondInvoice vendor: {6} amount: {7}" + "\nfirstInvoice+thirdInvoice vendor: {8} amount: {9}", firstInvoice.Vender, firstInvoice.Amount, secondInvoice.Vender, secondInvoice.Amount, thirdInvoice.Vender, thirdInvoice.Amount, i1.Vender, i1.Amount, i2.Vender, i2.Amount); } static void Main() { Tester t = new Tester(); t.Run(); } }
出力結果
firstInvoice vendor: Vendor1 amount: 1.2 fsecondInvoice vendor: Vendor1 amount: 3.4 thirdInvoice vendor: Vendor2 amount: 5.6 firstInvoice+fsecondInvoice vendor: Vendor1 amount: 4.6 firstInvoice+thirdInvoice vendor: amount: 0 続行するには何かキーを押してください . . .
練習 12-2
using System; public class Invoice { private string vendor; private double amount; public string Vender { 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); } else { 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 o) { if (!(o is Invoice)) { return false; } return this == (Invoice)o; } public override int GetHashCode() { return base.GetHashCode(); } } class Tester { public void Run() { Invoice firstInvoice = new Invoice("Vendor1", 1.2); Invoice secondInvoice = new Invoice("Vendor1", 1.2); Invoice thirdInvoice = new Invoice("Vendor2", 2.3); Console.WriteLine( "firstInvoice vendor: {0} amount: {1}" + "\nsecondInvoice vendor: {2} amount: {3}" + "\nthirdInvoice vendor: {4} amount: {5}" + "\nirstInvoice==secondInvoice : {6}" + "\nsecondInvoice==thirdInvoice : {7}" + "\nthirdInvoice==firstInvoice : {8}", firstInvoice.Vender, firstInvoice.Amount, secondInvoice.Vender, secondInvoice.Amount, thirdInvoice.Vender, thirdInvoice.Amount, firstInvoice == secondInvoice, secondInvoice == thirdInvoice, thirdInvoice == firstInvoice); } static void Main() { Tester t = new Tester(); t.Run(); } }
firstInvoice vendor: Vendor1 amount: 1.2 secondInvoice vendor: Vendor1 amount: 1.2 thirdInvoice vendor: Vendor2 amount: 2.3 irstInvoice==secondInvoice : True secondInvoice==thirdInvoice : False thirdInvoice==firstInvoice : False 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿