2

我想从类中请求数组并在程序中使用它。:base不工作

这是我的RandomArray

<!-- language: lang-js -->
public class RandomArray

{

    /// <summary>
    /// create a single random number generator
    /// </summary>
    static readonly Random generator = new Random();
    /// <summary>
    /// here are the random numbers stored
    /// </summary>
    int[] array;
    /// <summary>
    /// store the min, max used to generate the data
    /// </summary>
    readonly int min, max;
    /// <summary>
    /// Constructor only needs how the value limits
    /// </summary>
    /// <param name="min">The minimum value (typical 0)</param>
    /// <param name="max">The maximum value (example 100)</param>
    public RandomArray(int min, int max)
    {
        this.min=min;
        this.max=max;
        this.array=new int[0];
    }
    /// <summary>
    /// Fills the array with random numbers
    /// </summary>
    /// <param name="count">The number of data to generate</param>
    public void Fill(int count)
    {
        this.array=new int[count];
        // fill array with random integers
        for (int i=0; i<array.Length; i++)
        {
            array[i]=generator.Next(min, max);
        }
    }        
    /// <summary>
    /// Copy constructor if needed (optional)
    /// </summary>
    /// <param name="other">A RandomArray to copy the data from</param> 
    public RandomArray(RandomArray other)
    {
        this.min=other.min;
        this.max=other.max;
        this.array=(int[])other.array.Clone();
    }
    /// <summary>
    /// Provide the data
    /// </summary>

    public int[] Array { get { return array; } }
    /// <summary>
    /// Provide the limits used
    /// </summary>

    public int Min { get { return min; } }
    public int Max { get { return max; } }
    /// <summary>
    /// Creates a comma separated list of numbers like <c>[45,32,64,..]</c>
    /// </summary>
    public string ToStringList()
    {
        string[] parts=new string[array.Length];
        for (int i=0; i<parts.Length; i++)
        {
            parts[i]=array[i].ToString();
        }
        return "["+string.Join(",", parts)+"]";
    }
    /// <summary>
    /// Shows only the limits used
    /// </summary>
    public override string ToString()
    {
        return string.Format("RandomArray({0},{1})", min, max);
    }
}

<!-- language: none -->

我想对这个数组进行排序,所以我创建了另一个类:BubbleSort。

<!-- language: c#-->
class BubbleSort : RandomArray // inherit from RandomArray Class
    {

        public void Bubble() 
        {
            int temp;

            for (int j = 1; j <= Array.Length - 2; j++)
            {
                for (int i = 0; i <= Array.Length - 2; i++)
                {
                    if (Array[i] > Array[i + 1])
                    {
                        temp = Array[i + 1];
                        Array[i + 1] = Array[i];
                        Array[i] = temp;
                    }

                }

            }
        }
    }
}
<!-- language: none -->

最后一件事是按钮:

<!-- language: c#-->

private void button2_Click(object sender, EventArgs e)

        {

            BubbleSort p = new BubbleSort();
            p.Bubble();
        }
<!-- language: none -->

如何在同一个数组上操作(排序)?现在我有错误:'MyProgram' does not contain a constructor that takies 0 arguments. 请帮助,问候:)

4

3 回答 3

1

正如错误消息告诉您的那样,您的RandomArray类只有一个接受两个参数的构造函数:RandomArray(int min, int max). 这意味着您的派生类的构造函数必须将这些参数传递给基类的构造函数。

class BubbleSort : RandomArray 
{
     public BubbleSort(int min, int max)
         : base(min, max)
     { }
}

您还可以使用不同的构造函数重载来传递默认参数:

class BubbleSort : RandomArray 
{
     public BubbleSort(int min, int max)
         : base(min, max)
     { }

     public BubbleSort()
         : this(1, 10) 
     { }
}

然而,更重要的是继承在这里是错误的方法:“冒泡排序”不是数组。“冒泡排序”是一种“排序算法”。

换句话说,如果有的话,您可以编写如下内容:

interface ISortAlgorithm<T>
{
     void Sort(T[] array);
}

然后定义BubbleSort为:

class BubbleSort : ISortAlgorithm<int>
{
     public void Sort(int[] array) 
     { 
         ...
     }
}
于 2013-11-11T09:16:08.030 回答
0

如果您已经制作了 sort 方法,我会将 sort 方法包含在您的数组类中:

public void Bubble() 
{
    int temp;

    for (int j = 1; j <= array.Length - 2; j++)
    {
        for (int i = 0; i <= array.Length - 2; i++)
        {
            if (array[i] > array[i + 1])
            {
                temp = array[i + 1];
                array[i + 1] = array[i];
                array[i] = temp;
            }

        }

    }
}

然后你可以像这样使用它:

RandomArray myArray = new RandomArray(0,5);
myArray.Bubble();
于 2013-11-11T09:16:17.667 回答
0

选项1:

这里的问题是随机函数缺少零参数的构造函数

public class RandomArray
{
   public RandomArray()
   {}
}

我想你忘了调用函数,你可以像这样摆脱错误

 public void Bubble() :base()
 {

选项 2:

或避免现有代码出错

 public void Bubble() :base(1,10)
 {

选项 3:

或者你可以像这样修改你的子类构造函数

 public void Bubble(int i) :base(i)
 {
于 2013-11-11T09:10:27.740 回答