C# - 类变量声明

时间:2017-12-07 21:01:31

标签: c# class variables constructor

如何创建Boeing737的新实例并在以后的程序中使用它。例如,我希望能够创建5个Boeings,我是否必须像

那样定义它们
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;
    }
}

}

4 个答案:

答案 0 :(得分:2)

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

// 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<T>类型,它非常易于管理,并且在添加更多对象时会自行扩展:

List<Boeing737> boeings = new List<Boeing737>
{
    new Boeing737("A", 5),
    new Boeing737("B", 5),
    new Boeing737("C", 5),
    new Boeing737("D", 5)
};

如果您想创建一个可以包含不同类型平面的List,请坚持使用上一级类型:

List<Plane> planes = new List<Plane>
{
    new Boeing737("A", 5),
    new Boeing737("B", 5),
    new Boeing737("C", 5),
    new Boeing737("D", 5),
    new AirplaneX("D", 10, 350)
};

List也可以与LINQ一起使用,以方便其操作和过滤(更多信息here)。例如,按重量排序:

var planesSortedTons = planes.OrderBy(x => x.Tons).ToList();

仅选择Fuel > 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));
    }
}

答案 1 :(得分:0)

使用数组初始化:

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

答案 2 :(得分:0)

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

List<Boeing737> boeingList = new List<Boeing737>();
boeingList.add(new Boeing737() { param=value...});

然后,您可以通过索引,名称,循环访问它们等来访问它们。

我也会研究Linq

答案 3 :(得分:0)

如何使用C#Arrays。

命名空间ConsoleApps_examples {     课程     {         static void Main(string [] args)         {             //Console.WriteLine(“插入你正在使用的波音类型”);

        //string sname = Console.ReadLine();
        //int ifuel = int.Parse(Console.ReadLine());

        Console.WriteLine(" here are 5 different type of boeing:");

        string sname = "";
        int ifuel = 0;

        int i;
        int[] fuellist = new int[5] { 99, 98, 92, 97, 95 };
        var nameslist = new string[5]  { "XXA1", "XXA2", "XXA3","XXA4","XXA5"};

        //var arr3 = new string[] { "one", "two", "three" };

        for (i = 0; i < 5; i++)
        {
            ifuel = fuellist[i];
            sname = nameslist[i];
            Boeing737 boeing = new Boeing737(name: "Boeing737" + sname, fuel: ifuel, tons: 0);
            Console.WriteLine("{0} has {1} tons of fuel and weights {2}", boeing.Name, boeing.Fuel, boeing.Tons);

        }

        //Boeing737 boeing = new Boeing737(name: "Boeing737" + sname, fuel: ifuel, tons: 0);
        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;
    }
}

}

这是输出: List of 5 types of Boeing planes: