我可以在没有引用的情况下访问父变量吗?

Can I make parent Variables accessible without a reference?

我在继承另一个 class 的 class 中有一个 class 的列表。我希望能够访问父变量而不必使用引用(以提高可读性)。

    public class MyCode : OtherCode
    {
        public string label;
        public MyCode()
        {
            label = "somestring";
            colour = 5;
            Console.WriteLine("colour is: " + colour); // this is ok
            List<particle> parts = new List<particle>();
            parts.Add( new particle() );
        }

        public class particle //I'd like this class to have access to the parent variables
        {
            public particle()
            {
                Console.WriteLine("The colour is: " + colour); // can this be accessed without using a parent reference?
                //Console.WriteLine("The label is: " + label); // unaccessable
                Console.ReadLine();
            }
        }
    }

    public class OtherCode
    {
        public int colour = 1;
        public OtherCode()
        {
            //I didn't do it
        }
    }

是否可以通过这种方式继承父变量,这样我就可以在粒子方法中输入它们 'as is'?

对这个令人困惑的例子表示歉意,我想我是因为认为我可以在这种情况下使用继承而感到困惑.. 我想知道是否可以让嵌套的 class(粒子)访问标签和颜色,而无需在构造时传递对它的引用并在整个地方使用 Parent.variable

这是我确定何时使用继承的规则。

尝试描述问题并确定句子中的动词。

例如,这里正确的句子是什么?

因此,由于懒惰或其他原因,您不能使用继承。每个概念都有它的用例,避免 variable/property 声明不是 inheritance.

的目标