【发布时间】:2014-02-23 01:57:18
【问题描述】:
新手在这里寻求帮助。
目前我正在使用类,为此,我正在使用类根据我的产品类对象打印出 5 个直接对象。
这是打印我的物品的代码:
Console.WriteLine("1." + prod1.GetDetails() + "\n\n");
Console.WriteLine("2." + prod2.GetDetails() + "\n\n");
Console.WriteLine("3." + prod3.GetDetails() + "\n\n");
Console.WriteLine("4." + prod4.GetDetails() + "\n\n");
Console.WriteLine("5." + prod5.GetDetails() + "\n\n");
查看我的代码,感觉非常多余,因为只有两个字符发生了变化:第一个双引号中显示的项目编号,然后是区分我使用我的类对象打印的“产品”的数字产品(我是新手,希望我能正确传达这一点)。
我尝试创建一种方法来迭代它,因为这样会更干净且可扩展,但我的方法工作正常。
我最近的尝试:
public void DisplayOfferings()
{
for (int i = 1; i < 6; i++ )//prints items 1 to 5
{
Console.WriteLine(i + ". " + prod(i).GetDetails() + "\n\n");
} //iterate number
}
这就是我存储我要打印的产品的方式,我将它们放在类 PROGRAM 级别,因此它们可用于所有功能,因为我最终会使用它们几次 课堂节目 {
Product prod1 = new Product("iPod Shuffle", "The clip-and-go iPod shuffle. With buttons, VoiceOver, and playlists, it's the best of iPod shuffle. Available in seven colors.", 49.89, true);
Product prod2 = new Product("iPod Nano", "About the size of a credit card — and just 5.4 mm thin — iPod nano is the thinnest iPod ever made. Available in seven colors.", 149.89, true);
Product prod3 = new Product("iPod Classic", "With 160GB of storage, iPod classic is the take-everything-everywhere iPod. Available in Silver or Midnight", 249.89, false);
Product prod4 = new Product("iPod Red", "With 160GB of storage, iPod [RED] PRODUCT lets other fashion victims know you aren't just some mass consumer status concious drone, your a drone with a concious because Bono said so! Available in red.", 349.89, false);
Product prod5 = new Product("iTouch", "It's just like an iPhone accept that your parents obviously don't trust you with a phone yet.", 449.89, true);
【问题讨论】:
-
编辑了以上内容以显示我如何存储/构建要打印的项目/类对象。