没有类实例化的C#语法

问题描述 投票:-1回答:1

我正在研究C#中的排序,现在正在写一个quicksort,似乎语法不正确。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SortQuick
{
class quickSortAlgorithm
{
    private int[] array = new int[10];
    private int len;
    public static void quicksort(int left, int right)
    {
        int pivot;
        pivot = array[left];

        while(left < right)
        {
            while (array[left] <= pivot & (left<right)) {
                --right;
            }
            if(left != right)
            {
                array[left] = array[right];
                left++;
            }
            while((array[left]>= pivot) & (left>right)) {
                left++;
            }

            if(left!=right)
            {
                array[right] = array[left];
                right--;
            }
        }
        array[left] = pivot;
        pivot = left;
       if(left<pivot)
        {
            quicksort(left, pivot);
        }
       if(right>pivot)
        {
            quicksort(pivot+1, right);
        }
    }
    static void Main(string[] args)
    {
        quickSortAlgorithm qSort = new quickSortAlgorithm();
        int[] array = { 32, 43, 54, 65, 23, 56, 34, 78, 98, 12};
        qSort.array = array;
        qSort.len = qSort.array.Length;
        quicksort(0, qSort.len - 1);
        for (int j = 0; j &lt; qSort.len;++j) {
            Console.WriteLine(qSort.array[j]);
        }
    }
 }
}

编译器对函数中的数组和main循环中的转义条件不满意。我自己写的时候没有使用构造函数,但是我得到的错误是可以消失的,我只是不明白语法上的区别。

/*
*  C# Program to Implement Quick Sort
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sortQuickAlgorithm
{
class quickSortAlgorithm
{

    private int[] array = new int[20];
    private int len;

    public void QuickSortAlgorithm()
    {
        sort(0, len - 1);
    }

    public void sort(int left, int right)
    {
        int pivot, leftend, rightend;

        leftend = left;
        rightend = right;
        pivot = array[left];

        while (left < right)
        {
            while ((array[right] >= pivot) & amp; &amp; (left < right))
            {
                right--;
            }

            if (left != right)
            {
                array[left] = array[right];
                left++;
            }

            while ((array[left] >= pivot) & amp; &amp; (left < right))
            {
                left++;
            }

            if (left != right)
            {
                array[right] = array[left];
                right--;
            }
        }

        array[left] = pivot;
        pivot = left;
        left = leftend;
        right = rightend;

        if (left < pivot) { sort(left, pivot - 1); }
        if (right > pivot)
        {
            sort(pivot + 1, right);
        }
    }

    public static void Main()
    {
        quickSortAlgorithm q_Sort = new quickSortAlgorithm();

        int[] array = { 41, 32, 15, 45, 63, 72, 57, 43, 32, 52, 183 };
        q_Sort.array = array;
        q_Sort.len = q_Sort.array.Length;
        q_Sort.QuickSortAlgorithm();

        for (int j = 0; j & lt; q_Sort.len; j++)
        {
            Console.WriteLine(q_Sort.array[j]);
        }
        Console.ReadKey();
     }
  }
}

这就是错误信息。

Severity    Code    Description Project File    Line    Suppression State
Error   CS0120  An object reference is required for the non-static field, 
method, or property 'quickSortAlgorithm.array'  SortQuick    
C:\Program.cs   15  Active

Severity    Code    Description Project File    Line    Suppression State
Error   CS0103  The name 'j' does not exist in the current context  SortQuick    
C:\SortQuick\SortQuick\Program.cs   55  Active
Error   CS1026  ) expected  SortQuick   C:\SortQuick\SortQuick\Program.cs    
 55 Active
Error   CS1002  ; expected  SortQuick   C:\SortQuick\SortQuick\Program.cs    
55  Active
Error   CS1513  } expected  SortQuick   C:\SortQuick\SortQuick\Program.cs    
55  Active
Error   CS1026  ) expected  Miscellaneous Files C:\C#\Practice\Class1.cs     
32  Active
Error   CS1002  ; expected  Miscellaneous Files C:\C#\Practice\Class1.cs     
32  Active
Error   CS1513  } expected  Miscellaneous Files C:\C#\Practice\Class1.cs     
32  Active
Error   CS1026  ) expected  Miscellaneous Files C:\C#\Practice\Class1.cs     
43  Active
Error   CS1002  ; expected  Miscellaneous Files C:\C#\Practice\Class1.cs     
43  Active
Error   CS1513  } expected  Miscellaneous Files C:\C#\Practice\Class1.cs     
43  Active
Error   CS1026  ) expected  Miscellaneous Files C:\C#\Practice\Class1.cs     
76  Active
Error   CS1002  ; expected  Miscellaneous Files C:\C#\Practice\Class1.cs     
76  Active
 Error  CS1513  } expected  Miscellaneous Files C:\C#\Practice\Class1.cs     
 76 Active
Error   CS0103  The name 'lt' does not exist in the current context SortQuick    
C:\SortQuick\SortQuick\Program.cs   55  Active
Error   CS0201  Only assignment, call, increment, decrement, await, and new 
object expressions can be used as a statement   SortQuick    
C:\Program.cs   55  Active
 Error  CS0103  The name 'j' does not exist in the current context  SortQuick    
C:\Program.cs   56  Active
 Message    IDE1006 Naming rule violation: These words must begin with upper 
case 
characters: quickSortAlgorithm  SortQuick   C:\Program.cs   8   Active
 Message    IDE1006 Naming rule violation: These words must begin with upper 
 case 
 characters: quicksort  SortQuick   C:\Program.cs   12  Active
c# syntax
1个回答
0
投票

你正在执行 QuickSort(0, qSort.len - 1);static void Main(string[] args). 编译器认为你试图从静态作用域调用一个非静态方法。

我相信你真正想要的是执行 qSort.QuickSort(0, qSort.len - 1);.

这里的区别在于,编译器很清楚要调用的是QuickSort对象的哪个实例。QuickSort 方法上。

想象一下,如果你在Main方法中做了多个QuickSortAlgorithm对象。如果你只是调用 QuickSort 那么它怎么知道该为哪个实例执行这个方法呢?

我认为让你的算法实现在同一个类中,与你的静态程序入口点相同。main,是在混淆视听。如果你把事情分开,你可能会发现这个错误更容易识别。

using System;

namespace Algorithms
{
    public class Sort
    {
        static void Main(string[] args)
        {
            QuickSortAlgorithm qSort = new QuickSortAlgorithm();
            int[] array = { 32, 43, 54, 65, 23, 56, 34, 78, 98, 12};
            qSort.array = array;
            qSort.len = qSort.array.Length;

            // This isn't going to work, the QuickSortAlgorithm class doesn't have a static definition for this method.
            // Beyond that, our implementation of the QuickSort method references instance variables, which are not referencable from a static scope.
            QuickSortAlgorithm.QuickSort(0, qSort.len - 1);

            for (int j = 0; j < qSort.len;++j)
            {
                Console.WriteLine(qSort.array[j]);
            }
        }
    }

    public class QuickSortAlgorithm
    {
        // These are instance variables; i.e. they belong to an instance of QuickSortAlgorithm, not the static class.
        public int[] array = new int[10]; // <-- changed to public
        public int len; // <-- changed to public
        public void QuickSort(int left, int right)
        {
            int pivot;
            pivot = array[left];

            while(left < right)
            {
                while (array[left] <= pivot & (left<right)) {
                --right;
                }

                if(left != right)
                {
                    array[left] = array[right];
                    left++;
                }
                while((array[left]>= pivot) & (left>right))
                {
                    left++;
                }

                if(left!=right)
                {
                    array[right] = array[left];
                    right--;
                }
            }

            array[left] = pivot;
            pivot = left;

            if(left<pivot)
            {
                QuickSort(left, pivot);
            }

            if(right>pivot)
            {
                QuickSort(pivot+1, right);
            }
        }
    }
}

这里有一点比较清楚 我们真正想要的是不要调用静态的 QuickSortAlgorithm.QuickSort(0, qSort.len - 1);的方法,而是通过 qSort.QuickSort(0, qSort.len - 1);

© www.soinside.com 2019 - 2024. All rights reserved.