C# 创建类的唯一实例时出现问题
我一直在研究一个实现不同排序方法的类。C# 创建类的唯一实例时出现问题,c#,class,sorting,instance,C#,Class,Sorting,Instance,我一直在研究一个实现不同排序方法的类。 myTest.bubblesort(sortMe,false)以升序返回排序列表。我已经检查过这种行为了,效果很好backTest.bubblesort(sortMe,true)返回按降序排序的相同列表。我已经检查过这种行为,它是正确的 我遇到的麻烦是TestSorting的backTest实例的行为就像是对myTest实例的引用。当backTest对象被更改时,它还会修改myTest对象。换句话说,它们并不像我预期的那样是唯一的实例。有人能解释一下原因吗
myTest.bubblesort(sortMe,false)
以升序返回排序列表。我已经检查过这种行为了,效果很好backTest.bubblesort(sortMe,true)
返回按降序排序的相同列表。我已经检查过这种行为,它是正确的
我遇到的麻烦是TestSorting
的backTest
实例的行为就像是对myTest
实例的引用。当backTest
对象被更改时,它还会修改myTest
对象。换句话说,它们并不像我预期的那样是唯一的实例。有人能解释一下原因吗
class Program
{
static void Main(string[] args)
{
int[] sortMe = Sorting.GenerateTestArray(10, 100);
TestSorting<int> myTest = new TestSorting<int>();
TestSorting<int> backTest = new TestSorting<int>();
int[] test = myTest.BubbleSort(sortMe, false);
int[] testBack = backTest.BubbleSort(sortMe, true);
}
}
class TestSorting<T> where T : IComparable
{
public T[] BubbleSort(T[] sortMe, bool descending)
{
if (!descending)
return BubbleAscending(sortMe);
else
return BubbleDescending(sortMe);
}
private T[] BubbleAscending(T[] sortMe)
{
bool stopMe = true;
int stopRecurse = sortMe.Length - 1;
int optimizeMe = stopRecurse;
for (int i = 0; i < stopRecurse && stopMe; i++)
{
stopMe = false;
for (int j = 0; j < optimizeMe; j++)
{
if (sortMe[j].CompareTo(sortMe[j + 1]) > 0)
{
Swap(sortMe, j, j + 1);
stopMe = true;
}
}
optimizeMe--;
}
return sortMe;
}
private T[] BubbleDescending(T[] sortMe)
{
bool stopMe = true;
int stopRecurse = sortMe.Length - 1;
int optimizeMe = 0;
for (int i = 0; i < stopRecurse && stopMe; i++)
{
stopMe = false;
for (int j = stopRecurse; j > optimizeMe; j--)
{
if (sortMe[j].CompareTo(sortMe[j - 1]) > 0)
{
Swap(sortMe, j, j - 1);
stopMe = true;
}
}
optimizeMe++;
}
return sortMe;
}
}
类程序
{
静态void Main(字符串[]参数)
{
int[]sortMe=Sorting.GenerateTestArray(10100);
TestSorting myTest=新建TestSorting();
TestSorting backTest=新建TestSorting();
int[]test=myTest.BubbleSort(sortMe,false);
int[]testBack=backTest.BubbleSort(sortMe,true);
}
}
类TestSorting,其中T:IComparable
{
公共T[]泡泡排序(T[]排序,布尔下降)
{
如果(!下降)
返回气泡泄漏(sortMe);
其他的
返回气泡下降(sortMe);
}
私人T[]泡泡租赁(T[]sortMe)
{
bool stopMe=true;
int stopRecurse=sortMe.Length-1;
int optimizeMe=stopRecurse;
for(int i=0;i0
{
交换(sortMe,j,j+1);
stopMe=true;
}
}
我——;
}
返回空位;
}
私人T[]泡泡式搜索(T[]sortMe)
{
bool stopMe=true;
int stopRecurse=sortMe.Length-1;
int=0;
for(int i=0;ioptimizeMe;j--)
{
if(sortMe[j].CompareTo(sortMe[j-1])>0
{
交换(sortMe,j,j-1);
stopMe=true;
}
}
优化ME++;
}
返回空位;
}
}
但它们都是对sortMe的引用
您将sortMe传递给BubbleSort,但它们都是对sortMe的引用
将sortMe传递给BubbleSort您的类不是返回新数组,而是修改输入数组并返回对该数组的引用。如果您的输入是一次性的,您可以这样调用排序:
int[] test = myTest.BubbleSort(Sorting.GenerateTestArray(10, 100), false);
int[] testBack = backTest.BubbleSort(Sorting.GenerateTestArray(10, 100), true);
如果从sort类中删除return,则会更清楚。这样很明显,它是在修改第一个参数,而不是创建新数组。例如:
int[] test = Sorting.GenerateTestArray(10, 100);
myTest.BubbleSort(test, false);
int[] testBack = Sorting.GenerateTestArray(10, 100);
backTest.BubbleSort(testBack, true);
您的类不是返回新数组,而是修改输入数组并返回对该数组的引用。如果您的输入是一次性的,您可以这样调用排序:
int[] test = myTest.BubbleSort(Sorting.GenerateTestArray(10, 100), false);
int[] testBack = backTest.BubbleSort(Sorting.GenerateTestArray(10, 100), true);
如果从sort类中删除return,则会更清楚。这样很明显,它是在修改第一个参数,而不是创建新数组。例如:
int[] test = Sorting.GenerateTestArray(10, 100);
myTest.BubbleSort(test, false);
int[] testBack = Sorting.GenerateTestArray(10, 100);
backTest.BubbleSort(testBack, true);
如果它作用于相同的
sortMe
数据,为什么要采取不同的行动?将数据放在该类中,即使数据元素是不遵循的sameI,它们也将独立地进行操作。您的类没有状态(成员变量或属性),因此实例无需更改。myTest
按升序排序<代码>反向测试按降序排序。我希望在运行之后,int[]test
将以升序包含sortMe
,而int[]testBack
将以降序包含sortMe
。相反,首先,test
包含升序。但是当testBack
运行时,两者都包含testBack
的结果。我不明白为什么backTest
被视为对myTest
的引用,而不是唯一的对象。因为他们都返回了参数,并对其进行了适当的修改。它们都是对已修改到位的sortMe
的引用。myTest和backTest都没有被排序-它们没有数据。代码将外部数据作为sortme传递给它。如果它对相同的sortme
数据执行操作,为什么它的行为会有所不同?将数据放在该类中,即使数据元素是不遵循的sameI,它们也将独立地进行操作。您的类没有状态(成员变量或属性),因此实例无需更改。myTest
按升序排序<代码>反向测试按降序排序。我希望在运行之后,int[]test
将以升序包含sortMe
,而int[]testBack
将以降序包含sortMe
。相反,首先,test
包含升序。但是当testBack
运行时,两者都包含testBack
的结果。我不明白为什么backTest
被视为对myTest
的引用,而不是唯一的对象。因为他们都返回了参数,并对其进行了适当的修改。它们都是对已修改到位的sortMe
的引用。myTest和backTest都没有被排序-它们没有数据。代码将外部数据作为数据传递给它