C#-类变量的声明

C#-类变量的声明,c#,class,variables,constructor,C#,Class,Variables,Constructor,如何创建一个新的Boeing737实例,并在以后的程序中使用它。例如,我希望能够创建5个Boeing,我必须像这样定义它们吗 Boeing737 boeing1 = new Boeing737(name: "Boeing737" + Console.ReadLine(),fuel: int.Parse(Console.ReadLine()) , tons: 0); Boeing737 boeing2 = new Boeing737(name: "Boeing737" + Console.R

如何创建一个新的Boeing737实例,并在以后的程序中使用它。例如,我希望能够创建5个Boeing,我必须像这样定义它们吗

Boeing737 boeing1 =  new Boeing737(name: "Boeing737" + Console.ReadLine(),fuel: int.Parse(Console.ReadLine()) , tons: 0);

Boeing737 boeing2 =  new Boeing737(name: "Boeing737" + Console.ReadLine(),fuel: int.Parse(Console.ReadLine()) , tons: 0);
等等。。。 还是有更简单的方法? 另一个问题,例如,我可以将boeing1的所有属性分配给什么

这是我目前的代码:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Insert the type of boeing that u are using");
        Boeing737 boeing = new Boeing737(name: "Boeing737" + Console.ReadLine(),fuel: int.Parse(Console.ReadLine()) , tons: 0);
        Console.WriteLine("{0} has {1} tons of fuel and weights {2}", boeing.Name, boeing.Fuel, boeing.Tons);
        Console.ReadKey();

    }
}

public class Planes
{
    public Planes(string name, int fuel, int tons)
    {
        Name = name;
        Fuel = fuel;
        Tons = tons;
    }
    public int Tons;
    public int Fuel;
    public string Name { private set; get; }
}
class Boeing737 : Planes
{
    public Boeing737(string name, int fuel, int tons) : base(name, fuel, tons)
    {
        Tons = 700;
    }
}

}使用阵列初始化:

var boeings = new []
  {
    new Boeing737("name", 123, 1000),
    new Boeing737("name", 123, 1000),
    new Boeing737("name", 123, 1000),
    new Boeing737("name", 123, 1000),
  };

使用阵列初始化:

var boeings = new []
  {
    new Boeing737("name", 123, 1000),
    new Boeing737("name", 123, 1000),
    new Boeing737("name", 123, 1000),
    new Boeing737("name", 123, 1000),
  };

我会创建一个Boeing737的列表,我不会直接从控制台输入

List<Boeing737> boeingList = new List<Boeing737>();
boeingList.add(new Boeing737() { param=value...});
List boeingList=new List();
添加(新的Boeing737(){param=value…});
然后,您可以通过索引、名称、循环等方式访问它们


我也会研究Linq,我会创建一个Boeing737列表,我不会直接从控制台输入

List<Boeing737> boeingList = new List<Boeing737>();
boeingList.add(new Boeing737() { param=value...});
List boeingList=new List();
添加(新的Boeing737(){param=value…});
然后,您可以通过索引、名称、循环等方式访问它们


我还将研究Linq好吧,让我们从稍微改进代码开始:

// the class represents a single object, give it a
// singular name
public class Plane
{
    // get before set, it's not mandatory but give yourself
    // some basic coding rules to improve code maintainability
    // and readability
    // avoid public members, implementing properties is always
    // a good choice for future extensions and manipulations
    public int Fuel { get; private set; }
    public int Tons { get; private set; }
    public string Name { get; private set; }

    public Plane(string name, int fuel, int tons)
    {
        Name = name;
        Fuel = fuel;
        Tons = tons;
    }
}

// if your inheritance stops here, you could set a
// sealed attribute
public sealed class Boeing737 : Plane
{
    // no need to set the property twice, you are already
    // calling the base constructor, pass him the fixed
    // tons value of 700...
    public Boeing737(string name, int fuel) : base(name, fuel, 700)
    {
    }
}
现在,关于实例化,请选择泛型类型,它非常容易管理,并且在添加更多对象时会自我扩展:

List<Boeing737> boeings = new List<Boeing737>
{
    new Boeing737("A", 5),
    new Boeing737("B", 5),
    new Boeing737("C", 5),
    new Boeing737("D", 5)
};
列表
还可以与LINQ一起使用,以方便其操作和过滤(更多信息)。例如,按重量排序:

var planesSortedTons = planes.OrderBy(x => x.Tons).ToList();
仅选择燃油>10的飞机:

var planesFuel10 = planes.Where(x => x.Fuel > 10).ToList();
另一方面,如果您想通过控制台输入填充大量数据,则需要构建一个无限循环(例如
while(true)
)并通过添加填充列表:

static void Main(string[] args)
{
    List<Boeing737> boeings = new List<Boeing737>();

    String input;

    while (true)
    {
        Consol.WriteLine("Enter name:");
        input = Console.ReadLine();

        if (input.ToLowerInvariant() == "stop")
            break;

        String name = input.Trim();

        Consol.WriteLine("Enter fuel:");
        input = Console.ReadLine();

        if (input.ToLowerInvariant() == "stop")
            break;

        Int32 fuel;

        try
        {
            fuel = Int32.Parse(input.Trim());
        }
        catch
        {
            Console.WriteLine("Wrong input, stopping!");
            break;
        }

        boeings.Add(new Boeing737(name, fuel));
    }
}
static void Main(字符串[]args)
{
List boeings=新列表();
字符串输入;
while(true)
{
控制台写入线(“输入名称:”);
input=Console.ReadLine();
if(input.ToLowerInvariant()=“停止”)
打破
字符串名称=input.Trim();
控制台写入线(“输入燃料:”;
input=Console.ReadLine();
if(input.ToLowerInvariant()=“停止”)
打破
Int32燃料;
尝试
{
fuel=Int32.Parse(input.Trim());
}
抓住
{
Console.WriteLine(“输入错误,正在停止!”);
打破
}
添加(新的Boeing737(名称、燃料));
}
}

好吧,让我们从稍微改进代码开始:

// the class represents a single object, give it a
// singular name
public class Plane
{
    // get before set, it's not mandatory but give yourself
    // some basic coding rules to improve code maintainability
    // and readability
    // avoid public members, implementing properties is always
    // a good choice for future extensions and manipulations
    public int Fuel { get; private set; }
    public int Tons { get; private set; }
    public string Name { get; private set; }

    public Plane(string name, int fuel, int tons)
    {
        Name = name;
        Fuel = fuel;
        Tons = tons;
    }
}

// if your inheritance stops here, you could set a
// sealed attribute
public sealed class Boeing737 : Plane
{
    // no need to set the property twice, you are already
    // calling the base constructor, pass him the fixed
    // tons value of 700...
    public Boeing737(string name, int fuel) : base(name, fuel, 700)
    {
    }
}
现在,关于实例化,请选择泛型类型,它非常容易管理,并且在添加更多对象时会自我扩展:

List<Boeing737> boeings = new List<Boeing737>
{
    new Boeing737("A", 5),
    new Boeing737("B", 5),
    new Boeing737("C", 5),
    new Boeing737("D", 5)
};
列表
还可以与LINQ一起使用,以方便其操作和过滤(更多信息)。例如,按重量排序:

var planesSortedTons = planes.OrderBy(x => x.Tons).ToList();
仅选择燃油>10的飞机:

var planesFuel10 = planes.Where(x => x.Fuel > 10).ToList();
另一方面,如果您想通过控制台输入填充大量数据,则需要构建一个无限循环(例如
while(true)
)并通过添加填充列表:

static void Main(string[] args)
{
    List<Boeing737> boeings = new List<Boeing737>();

    String input;

    while (true)
    {
        Consol.WriteLine("Enter name:");
        input = Console.ReadLine();

        if (input.ToLowerInvariant() == "stop")
            break;

        String name = input.Trim();

        Consol.WriteLine("Enter fuel:");
        input = Console.ReadLine();

        if (input.ToLowerInvariant() == "stop")
            break;

        Int32 fuel;

        try
        {
            fuel = Int32.Parse(input.Trim());
        }
        catch
        {
            Console.WriteLine("Wrong input, stopping!");
            break;
        }

        boeings.Add(new Boeing737(name, fuel));
    }
}
static void Main(字符串[]args)
{
List boeings=新列表();
字符串输入;
while(true)
{
控制台写入线(“输入名称:”);
input=Console.ReadLine();
if(input.ToLowerInvariant()=“停止”)
打破
字符串名称=input.Trim();
控制台写入线(“输入燃料:”;
input=Console.ReadLine();
if(input.ToLowerInvariant()=“停止”)
打破
Int32燃料;
尝试
{
fuel=Int32.Parse(input.Trim());
}
抓住
{
Console.WriteLine(“输入错误,正在停止!”);
打破
}
添加(新的Boeing737(名称、燃料));
}
}
使用C#数组怎么样

名称空间控制台APS_示例 { 班级计划 { 静态void Main(字符串[]参数) { //Console.WriteLine(“插入您正在使用的波音类型”)

//字符串sname=Console.ReadLine();
//int-ifuel=int.Parse(Console.ReadLine());
WriteLine(“这里有5种不同类型的波音:”);
字符串sname=“”;
int-ifuel=0;
int i;
int[]fuelist=newint[5]{99,98,92,97,95};
var nameslist=新字符串[5]{“XXA1”、“XXA2”、“XXA3”、“XXA4”、“XXA5”};
//var arr3=新字符串[]{“一”、“二”、“三”};
对于(i=0;i<5;i++)
{
ifuel=加油工[i];
sname=名称列表[i];
Boeing737波音=新的Boeing737(名称:“Boeing737”+sname,燃料:ifuel,吨:0);
Console.WriteLine({0}有{1}吨燃料和重量{2}),boeing.Name,boeing.fuel,boeing.tons);
}
//Boeing737波音=新的Boeing737(名称:“Boeing737”+sname,燃料:ifuel,吨:0);
Console.ReadKey();
}    
}
公务舱飞机
{
公共飞机(字符串名称、整数燃料、整数吨)
{
名称=名称;
燃料=燃料;
吨=吨;
}
公共整数吨;
公共燃料;
公共字符串名称{private set;get;}
}
波音737类:飞机
{
公共Boeing737(字符串名称,整数燃料,整数吨):基本(名称,燃料,吨)
{
吨=700;
}
}
}

这是输出: 使用C#数组怎么样

名称空间控制台APS_示例 { 班级计划 { 静态void Main(字符串[]参数) { //Console.WriteLine(“插入您正在使用的波音类型”)

//字符串sname=Console.ReadLine();
//int-ifuel=int.Parse(Console.ReadLine());
WriteLine(“这里有5种不同类型的波音:”);
字符串sname=“”;
int-ifuel=0;
int i;
int[]fuelist=newint[5]{99,98,92,97,95};
var nameslist=新字符串[5]{“XXA1”、“XXA2”、“XXA3”、“XXA4”、“XXA5”};
//var arr3=新字符串[]{“一”、“二”、“三”};
对于(i=0;i<5;i++)
{
ifuel=加油工[i];
sname=namesli