C# 是否可以创建创建类实例的循环?
我想创建一个控制台应用程序,它读取userinput值并创建一个类的多个实例,然后在所有类实例中运行void say()。 代码如下:C# 是否可以创建创建类实例的循环?,c#,C#,我想创建一个控制台应用程序,它读取userinput值并创建一个类的多个实例,然后在所有类实例中运行void say()。 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleApplication3 { class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int botsnumber = 0;
Console.WriteLine("Type number: ");
botsnumber = Int32.Parse(Console.ReadLine());
for (int i = 0; i < botsnumber; i++)
{
//Here generate bots: Bot b1 = new Bot(); Bot b2 = new Bot();
// Bot b = new Bot();
}
b.say();// b1.say(); b2.say(); b3.say(); ...
}
}
class Bot
{
public static void say()
{
Console.WriteLine("Hello!");
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统线程;
使用System.Threading.Tasks;
命名空间控制台应用程序3
{
班级计划
{
静态void Main(字符串[]参数)
{
整数=0;
Console.WriteLine(“类型编号:”);
botsnumber=Int32.Parse(Console.ReadLine());
for(int i=0;i
将实例放入一个列表中,填充,然后循环列表并调用say
botsnumber = Int32.Parse(Console.ReadLine());
var list = new List<Bot>();
for (int i = 0; i < botsnumber; i++)
{
Bot b = new Bot();
list.Add(b);
}
list.ForEach(b => b.say());
将实例放入一个列表中,填充它,然后循环列表并调用
say
botsnumber = Int32.Parse(Console.ReadLine());
var list = new List<Bot>();
for (int i = 0; i < botsnumber; i++)
{
Bot b = new Bot();
list.Add(b);
}
list.ForEach(b => b.say());
我想是这样吧
public static List<Bot> bots = new List<Bot>();
...
for (int i = 0; i < botsnumber; i++)
{
bots.Add(new Bot);
}
...
bots[0].Say();
bots[1].Say();
publicstaticlist bots=newlist();
...
for(int i=0;i
我想是这样吧
public static List<Bot> bots = new List<Bot>();
...
for (int i = 0; i < botsnumber; i++)
{
bots.Add(new Bot);
}
...
bots[0].Say();
bots[1].Say();
publicstaticlist bots=newlist();
...
for(int i=0;i
您可以立即呼叫say,也可以在创建所有机器人后全部呼叫他们
for (int i = 0; i < botsnumber; i++)
{
//Here generate bots: Bot b1 = new Bot(); Bot b2 = new Bot();
Bot b = new Bot();
b.say();
}
// or
var bots = new List<Bot>();
for (int i = 0; i < botsnumber; i++)
{
//Here generate bots: Bot b1 = new Bot(); Bot b2 = new Bot();
bots.Add(new Bot());
}
foreach (var bot in bots)
{
bot.say();
}
for(int i=0;i
您可以立即呼叫say,也可以在创建所有机器人后全部呼叫他们
for (int i = 0; i < botsnumber; i++)
{
//Here generate bots: Bot b1 = new Bot(); Bot b2 = new Bot();
Bot b = new Bot();
b.say();
}
// or
var bots = new List<Bot>();
for (int i = 0; i < botsnumber; i++)
{
//Here generate bots: Bot b1 = new Bot(); Bot b2 = new Bot();
bots.Add(new Bot());
}
foreach (var bot in bots)
{
bot.say();
}
for(int i=0;i
我认为您希望在循环内部调用say()
,并使其成为非静态方法:
for (int i = 0; i < botsnumber; i++)
{
Bot b = new Bot();
b.Say();
}
class Bot
{
public void Say()
{
Console.WriteLine("Hello!");
}
}
for(int i=0;i
尽管请注意,在许多方面,创建一个立即丢弃的无状态对象实例是没有意义的,所以。。。在实际使用中,我要么期望某种状态,要么期望它是一种静态方法
或者,您可以先创建所有机器人程序,将它们累积到一个列表中,然后循环使用-同样,这是否合理取决于上下文,但是:
var bots = new List<Bot>(botsnumber);
for (int i = 0; i < botsnumber; i++)
{
bots.Add(new Bot());
}
// ...
foreach(var bot in bots)
{
bot.Say();
}
var bots=新列表(bots编号);
for(int i=0;i
我认为您希望在循环内部调用say()
,并使其成为非静态方法:
for (int i = 0; i < botsnumber; i++)
{
Bot b = new Bot();
b.Say();
}
class Bot
{
public void Say()
{
Console.WriteLine("Hello!");
}
}
for(int i=0;i
尽管请注意,在许多方面,创建一个立即丢弃的无状态对象实例是没有意义的,所以。。。在实际使用中,我要么期望某种状态,要么期望它是一种静态方法
或者,您可以先创建所有机器人程序,将它们累积到一个列表中,然后循环使用-同样,这是否合理取决于上下文,但是:
var bots = new List<Bot>(botsnumber);
for (int i = 0; i < botsnumber; i++)
{
bots.Add(new Bot());
}
// ...
foreach(var bot in bots)
{
bot.Say();
}
var bots=新列表(bots编号);
for(int i=0;i
假设
是静态的,您不需要创建实例来调用它。您到底有什么问题?创建实例?对但是只有在你之前声明了变量(或者对它们有额外的引用)的情况下,你的显示方式才会使它们超出范围。你想要什么还不清楚。您是否希望能够创建一组Bot
的实例,然后只需对其中一个实例的say()
调用say()
就可以对所有其他实例进行调用?say
是静态的,您不需要创建实例来调用它。您的问题到底是什么?创建实例?对但是只有在你之前声明了变量(或者对它们有额外的引用)的情况下,你的显示方式才会使它们超出范围。你想要什么还不清楚。您是否希望能够创建一组Bot
的实例,然后只需对其中一个实例的say()
进行一次调用,就可以同时调用所有其他实例的say()
?