開発環境
- 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
{
public class Dog
{
private int weight;
private string name;
public int Weight
{
get { return weight; }
set { weight = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public Dog(int weight, string name)
{
this.weight = weight;
this.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 };
Console.WriteLine("犬一覧");
foreach (Dog dog in dogs)
{
Console.WriteLine("名前: {0}, 重さ: {1}",
dog.Name, dog.Weight);
}
}
static void Main()
{
Tester t = new Tester();
t.Run();
}
}
}
入出力結果(Console Window)
犬一覧 名前: milo, 重さ: 26 名前: Frisky, 重さ: 10 名前: Laika, 重さ: 50 続行するには何かキーを押してください . . .
0 コメント:
コメントを投稿