【问题标题】:c# Accessing my array inside a class constructorc# 在类构造函数中访问我的数组
【发布时间】:2019-10-17 07:31:41
【问题描述】:

我正在学习 c#,并决定尝试创建一个功能性的机会游戏,一部分一部分。 我之前创建了一个方法,该方法会创建一个随机(但可能效率低下)自然数数组,该数组不会出现多次。

但是,当我尝试将 OOP 拼凑在一起时,我意识到如果我创建多个这样的数组,它们将是对象,因此应该由一个类创建。

我在构造函数中创建了数组。然而,我无法从构造函数的类中或完全在另一个类中访问这个数组。

class randomArray
    {

        Random rng = new Random();
        protected int amountOfNumbers;
        protected int rangeOfNumbers;

        public randomArray(int amountOfNumbers, int rangeOfNumbers)
        {
            this.amountOfNumbers = amountOfNumbers;
            this.rangeOfNumbers = rangeOfNumbers;
            int[] randomizedArray = new int[amountOfNumbers];

            for (int i = 0; i < amountOfNumbers; i++)
            {
                randomizedArray[i] = rng.Next(1, rangeOfNumbers + 1);
                // A test to ensure that each new number generate is not 
                already part of the array.
                for (int j = 0; j < i; j++)
                {
                    while (randomizedArray[i] == randomizedArray[j])
                    {
                        randomizedArray[i] = rng.Next(1, rangeOfNumbers + 1);
                        j = 0;
                    }
                    if (randomizedArray[i] != randomizedArray[j])
                        continue;

                }
            }
        }

        public int RangeOfNumbers { get; set; }
        public int AmountOfNumbers { get; set; }

我认为我要么不了解 OOP 的基础知识,要么不了解如何使用类。

【问题讨论】:

  • 了解Properties
  • 除了您的数组之外,您还实现了两个相互不相关的成员 amountOfNumbers 和 AmountOfNumbers。如果您使用 {get;set;} 语法,支持属性的字段将自动创建,您无需手动创建。

标签: c# class constructor


【解决方案1】:

让你的array成为实际class的成员,即property

public class randomArray
{

    public int[] RandomizedArray { get; set; }

    ...

大约在这个时候,您可能应该仔细阅读此内容

更新

public randomArray(int amountOfNumbers, int rangeOfNumbers)
{
    ...
    RandomizedArray = new int[amountOfNumbers]
    // do stuff here

【讨论】:

  • 哦,我明白你的意思了。如何将此属性链接到我在构造函数中创建的数组?
【解决方案2】:

尽管@TheGeneral 的回复包含了解决方案,并为您提供了更多有关 OOP 学习的文档。 我认为最好解释一下为什么您的代码不起作用。


OOP 中的所有内容都有一个范围(某个可用的“区域”),对于 OOP 中的大多数内容,该范围由括号括起来。 在这种情况下,范围基于构造函数,这导致括号中声明的变量仅在括号内可用。除非您使用类变量或属性等“外部”链接。

public class Example
{
    // this is a class variable, this variable is now reachable from outside the class 
    // definition.
    public int aClassVariable;

    // this is a class property which because we added the get and set calls generate 
    // automatically an get and set method (internally)
    public bool aClassProperty { get; set; }

    public Example()
    {
        // to set the class variable and property you just give them a value.
        aClassVariable = 42;
        aClassProperty = true;

        // this variable is not available outside the scope of this function, 
        // this is because you declared the variable inside this function.
        // So the variable is only available inside this function as long as this 
        // function runs (or as it is called "is in scope").
        int[] arr = new int[10]; 
    }
}

还要注意变量和属性的区别,变量是每个OOP语言都包含的东西。 但属性实际上是变量的扩展,可以通过定义 get 和 set 方法来修改访问和设置。

我强烈建议阅读TheGeneral 的答案链接的文档,因为它包含有关 OOP 和 C# 本身的复杂性的更多信息。

【讨论】:

    猜你喜欢
    • 2011-09-05
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 2015-05-09
    • 2011-05-08
    • 1970-01-01
    • 2010-09-26
    • 2014-09-07
    相关资源
    最近更新 更多