OOP C#:如何编写一个在WinForm和Console中都能工作的吐出数组的方法

本文关键字:Console 工作 数组 方法 WinForm 何编写 一个 OOP | 更新日期: 2023-09-27 17:59:50

我正在尝试编写一个在winform和控制台中都可用的方法。

这是我的代码:

  public class BeerKoozie
    {
        public bool IsSinging = false;
        //Your classes aren't working because you didn't have a constructor method.
        public BeerKoozie()
        {
            for (int beerCount = 0; beerCount < 100; beerCount++)
            {
                beerCollection[beerCount] = new Beer();
                if (beerCount == 99)
                {
                    IsSinging = true;
                }
            }
        }


        //needs a collection representing one hundred beers.
        Beer[] beerCollection = new Beer[100];
        // needs a notion of a current beer
        public int CurrentBeer = 99;
        // needs a notion of going to the next beer.
        public string DrinkAbeerAndSingTheSong()
        {
            //BeerKoozie.bee
            //foreach (Beer value in beerCollection)
            //foreach (Beer value in beerCollection)
            do(Beer i in beerCollection)
            {
                beerCollection[CurrentBeer].IsFull = false;
                CurrentBeer--;

            } while (CurrentBeer > 0);
            return CurrentBeer.ToString() + "... beers on the wall";
        }

每当我尝试在.ToString的末尾打印出beerCollection[CurrentBeer]时,方法的名称就会打印出来。那么,在保持业务端和视图分离的同时,对整个数组进行迭代的平台无关方法应该使用哪种模式呢?我可以很容易地编写一个只适用于控制台或winform的方法,该方法具有特定的字符串句柄(我的代码不可编译,因为我在处理方法时破坏了它,无法恢复到以前的状态。我现在也有功能齐全的代码,所以无法重现问题。)

OOP C#:如何编写一个在WinForm和Console中都能工作的吐出数组的方法

从哪里开始??!!!??我不认为有任何可能的方式来编译。。。。我将逐步完善我的答案。

好的,用你的循环,改为这样做:

foreach(Beer i in beerCollection)
{
    i.IsFull = false;
} 

请注意,while已经不见了(它与集合的foreach迭代无关),您的公开可见索引/计数器也不见了。

接下来,构造函数中的循环:

for (int i = 0; i < 100; i++)
{
    beerCollection[i] = new Beer();
}
IsSinging = true;

beerCount更改为i-这是一个语义参数,i是引用循环索引器的标准方式,而且它的键入时间要短得多,不需要长的描述性名称。注意,IsSinging已经被移到了循环之外——你只需要在循环结束时设置它,你不需要一个条件语句来判断是否是时候设置它了。

最后,回答您的问题:

如何编写一个在WinForm和Console中都能工作的吐出数组的方法?

这在两种情况下都有效——数组是一种语言构造,与您设置的项目/可执行文件类型完全无关。BeerKoozie是一个可以在其中任何一个中使用的类-只需将一个新的类库项目添加到您的解决方案中,将BeerKoozie类添加到该新项目中,然后您就可以从引用该新类项目的任何其他项目/程序集中调用它。

有办法多次返回吗?

不,没有。你把自己搞混了——CurrentBeer.ToString() + "... beers on the wall";行需要在循环中,否则你要做的就是调用函数,遍历整个集合,只返回一次。我建议你使用类似于以下的方法(我根据你的要求使用do/while循环):

public string DrinkAbeerAndSingTheSong()  
{  
    CurrentBeer = beerCollection.Length - 1;  //take 1 off, remember the array index is zero based   
    //StringBuilder sb = new StringBuilder();
    do
    {
        beerCollection[CurrentBeer].IsFull = false;
        //sb.AddLine(CurrentBeer + "... beers on the wall");
        //Console.Writeline(CurrentBeer + "... beers on the wall");
        CurrentBeer--;
    } while (CurrentBeer > 0);
    return CurrentBeer + "... beers on the wall";
    //sb.AddLine(CurrentBeer + "... beers on the wall");  //don't forget the last line of the song
    //return sb.ToString();
}

有了这个,你有几个选择,这取决于你试图实现的目标。您不想调用该方法100次来调整数组中啤酒的计数和状态,所以您应该在循环中完成所有工作。我想你要么想在动作发生时打印出歌曲的每一行,要么想返回整首歌,所以我已经包含了这两个内容——只需取消对适当代码的注释,然后播放即可。