【问题标题】:Why can't i assign a instance member variable in a class [duplicate]为什么我不能在类中分配实例成员变量[重复]
【发布时间】:2019-07-05 04:36:47
【问题描述】:

当我尝试在体积 = 长度 * 宽度 * 高度的类中分配实例成员变量时的第一个片段。我收到一条错误消息,指出字段初始化程序无法引用非静态字段、方法或属性“Box.height”。

namespace Myproject
{
    class Box
    {
        public int length;
        public int height;
        public int width;
        public int volume = length * width * height;
        public void DisplayBox()
        {
            Console.WriteLine("The height of box is length {0} * height {1} * width {2} and volume is {3}", length, height, width, volume =);
        }
    }
}

第二个代码片段,当我将所有变量设为静态时,它允许卷接受给定的任务,不确定如何解释自己,但有人可以向我解释。

namespace Myproject
{
    class Box
    {
        public  static int length;
        public static int height;
        public static int width;
        public static int volume = length * width * height;
        public void DisplayBox()
        {
            Console.WriteLine("The height of box is length {0} * height {1} * width {2} and volume is {3}", length, height, width, volume);
        }
    }
}

第三个片段为什么只有在允许将体积分配给变量进行计算的方法中?

namespace Myproject
{
    class Box
    {
        public int length;
        public int height;
        public int width;
        public int volume;
        public void DisplayBox()
        {
            Console.WriteLine("The height of box is length {0} * height {1} * width {2} and volume is {3}", length, height, width, volume = length * width * height);
        }
    }
}

【问题讨论】:

  • 请不要发这样的图片。做任何其他事情。形容它。包括代码。无论您想让别人阅读什么,都请将其作为文本包含在内。
  • 您尝试过使用属性吗?在此处将代码作为文本而不是图像发布。
  • public int Volume { get => length * width * height; } 并遵循@ScottHannen 的建议
  • volume 的赋值是“初始化”:它在实例创建时发生一次。但是当时高度和宽度等还没有值,所以编译器不会让你这样做。无论如何,它只会发生一次——这只是一次性的任务。 Kevin 的建议将volume 变成了一个“属性”:它不仅仅是一个值;它的代码可以在您每次查看时计算一个值。我想这才是你真正想要的。
  • 你好,但是为什么当变量变为静态时它允许初始化也卷接受给定的赋值但是当它是实例变量时,我可以分配卷的唯一方法是通过方法、构造函数或属性为什么是这样的,为什么我不能在作为实例成员变量时分配音量。

标签: c#


【解决方案1】:
class Box
    {
        public int Length { get; set; }
        public int Height { get; set; }
        public int Width { get; set; }
        public int Volume => Length * Width * Height;
        public void DisplayBox()
        {
            Console.WriteLine("The height of box is length {0} * height {1} * width {2} and volume is {3}", Length, Height, Width, Volume);
        }
        public Box(){}
        public Box(int length, int height, int width)
        {
            Length = length;
            Height = height;
            Width = width;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //Using custom constructor
            new Box(5,10,4).DisplayBox();
            //Using default constructor
            new Box {Length = 5, Height = 10, Width = 4}.DisplayBox();
            //Using class instance
            var box = new Box();
            box.Length = 5;
            box.Height = 10;
            box.Width = 4;
            box.DisplayBox();
            Console.ReadKey();
        }
    }

【讨论】:

    猜你喜欢
    • 2014-11-12
    • 2012-08-04
    • 2017-08-16
    • 1970-01-01
    • 2013-03-21
    • 2014-08-31
    • 1970-01-01
    • 2010-10-10
    • 2018-05-27
    相关资源
    最近更新 更多