C# 向另一个类请求数组
我想从类中请求数组并在程序中使用它<代码>:基地不工作 这是我的C# 向另一个类请求数组,c#,arrays,class,request,C#,Arrays,Class,Request,我想从类中请求数组并在程序中使用它:基地不工作 这是我的RandomArray课程 <!-- language: lang-js --> public class RandomArray { /// <summary> /// create a single random number generator /// </summary> static readonly Random generator = new Random
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 -->
公共类随机数组
{
///
///创建一个随机数生成器
///
静态只读随机生成器=新随机();
///
///这是存储的随机数
///
int[]数组;
///
///存储用于生成数据的最小值和最大值
///
只读int最小值,最大值;
///
///构造函数只需要知道值是如何限制的
///
///最小值(典型为0)
///最大值(示例100)
公共随机数组(最小整数,最大整数)
{
这个.min=min;
这个。max=max;
this.array=newint[0];
}
///
///用随机数填充数组
///
///要生成的数据数
公共空间填充(整数计数)
{
this.array=newint[count];
//用随机整数填充数组
对于(int i=0;i
类BubbleSort:RandomArray//从RandomArray类继承
{
公共空间泡沫()
{
内部温度;
对于(int j=1;j
私有无效按钮2\u单击(对象发送者,事件参数e)
{
BubbleSort p=新的BubbleSort();
p、 气泡();
}
如何对同一数组进行操作(排序)?现在出现错误:'MyProgram'不包含接受0个参数的构造函数。
请帮助,注意:)选项1:
这里的问题是随机函数缺少具有零参数的构造函数
public class RandomArray
{
public RandomArray()
{}
}
我想你忘了调用函数,你可以这样走出错误
public void Bubble() :base()
{
public void Bubble(int i) :base(i)
{
备选案文2:
或者避免现有代码出现错误
public void Bubble() :base(1,10)
{
备选案文3:
或者您可以像这样修改子类构造函数
public void Bubble() :base()
{
public void Bubble(int i) :base(i)
{
错误消息告诉您,
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);
}
如果您已经创建了一个arrayclass,我会将排序方法包括到您的arrayclass中:
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;
}
}
}
}
如果需要对数组进行排序,可以使用SortedList或泛型列表,当我使用:base all my class时很容易排序。存在诸如“无效令牌”和“语法错误”之类的错误。@user2971920-可能是因为额外的空间,如果我键入:base all my class正在broking.After BubbleSort()我写:base和我的类因错误而充满红色。非泛型类型“MyProgram.ISortAlgorithm”不能与此类型一起使用arguments@user2971920:您确定键入的
接口
声明正确吗?即接口ISortAlgorithm
?即“
”部件对于使其通用化很重要。是的,谢谢。它工作正常,但不在文本框中显示结果。我添加了界面,但在文本框中我有MyProgra.BubbleSort
而不是数字。它是按钮BubbleSort p=new BubbleSort();textBox1.Text=p.ToString()中的代码;
我必须重写此方法?@user2971920:ToString方法本身没有什么作用(默认情况下返回类名除外),除非显式重写。此外,您需要指定如何显示数据。而且TextBox
似乎不是显示数字列表的理想控件。最好在StackOverflow上发布一个新问题,并提供详细说明。
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();