開発環境
- Microsoft Windows 7 Home Premium (OS)
- Microsoft Visual C# 2010 Express Edition (IDE)
- 言語: C#
『初めてのC# 第2版』(Jesse Liberty+Brian MacDonald著、日向俊二訳、オライリー・ジャパン、2006年、ISBN978-487311-294-7)の 第10章(配列)10.9(練習問題)、練習10-1を解いてみる。
練習10-1.
コード
using System;
namespace Sample
{
class Dog
{
private int weight;
private string name;
public Dog(int weight, string name)
{
this.weight = weight;
this.name = name;
}
public override string ToString()
{
return "Weight; " + weight +
" Name: " + name;
}
}
class Tester
{
public void Run()
{
Dog milo = new Dog(26, "Milo");
Dog frisky = new Dog(10, "Frisky");
Dog laika = new Dog(50, "Laika");
Dog[] dogs = { milo, frisky, laika };
foreach (Dog dog in dogs)
{
Console.WriteLine(dog.ToString());
}
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
}
入出力結果(Console Window)
Weight; 26 Name: Milo Weight; 10 Name: Frisky Weight; 50 Name: Laika 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿