0

这是代码:

class Program
{
    static void Main(string[] args)
    {

      List<Item> cont1 = new List<Item>();
      cont1.Add(objA); //objA is local varible that has been defined. 
      cont1.Add(objB); //objB is local varible that has been defined. 
      cont1.Add(objC); //objC is local varible that has been defined. 
      cont1.Add(objD); //objD is local varible that has been defined. 
      cont1.Add(objE); //objE is local varible that has been defined.

      int count = cont1.Count; 

      List<Item> cont2 = GroupingA(cont1, count);

      for (int i = 0; i < cont1.Count; i++)
      {
         Console.Write(cont1[i].Name + " ");//output the item's name.

      }
      Console.WriteLine();
      Console.Write("Container 2: ");
      for (int i = 0; i < cont2.Count; i++)
      {
         Console.Write(cont2[i].Name + " ");//output the item's name.
      }
    }
}
public class GroupItem
{
    public List<Item> GroupingA (List<Item> obj, int count)
    {
        List<Item> temp = new List<Item>();
        for (int i = 0; i < count; i++)
        {
            if (i == count - 1)
            {
                break;
            }
            else if (temp.Count > 0 )
            {
                break;
            }
            else
            {
                for (int j = i + 1; j < count; j++)
                {
                    bool equal = obj[i].EqualFirstPhase(obj[j]);
                    if (equal == true)
                    {
                        if (temp.Exists(element => element == temp[j]))
                        {
                            continue;
                        }
                        else
                        {
                            temp.Add(obj[j]);
                            if (temp.Exists(element => element == obj[i]))
                            {
                                continue;
                            }
                            else
                            {
                                temp.Insert(0, obj[i]);
                            }
                            i--;

                        }

                    }
                }

            }
            for (int k = 0; k < count; k++)
            {
                for (int l = 0; l < temp.Count; l++)
                {
                    if (obj[k].Name.Contains(temp[l].Name))
                    {
                        obj.RemoveAt(k);
                        count--;

                    }
                }
                if (obj.Count < count)
                {
                    k = 0;

                }
            }


        }
        return temp;
    }
}

我想使用GroupingA方法重新组合cont1into cont2。但是,有一个错误。

当前上下文中不存在名称“GroupingA”。

谁能帮帮我???OOP 真的很弱,尤其是命名。

4

2 回答 2

0

您需要使用:-

GroupItem grpItem  = new GroupItem(); 
List<Item> cont2 = grpItem.GroupingA(cont1, count);

原因:-

您的 GroupingA 方法是非静态的,并在 class 中定义GroupingItem。而且您正试图从您的 Program 类中访问它,就好像该方法GroupingA是该类的一部分一样。要从其他类访问非静态方法,您需要Instantiate该类并使用该对象来访问该类。如果是静态方法,您可以使用.该特定类上的运算符直接访问它们。前 Class.Method(...)

于 2013-04-18T02:56:28.177 回答
0

该行 List<Item> cont2 = GroupingA(cont1, count);正在类中被调用Program

如果没有说明方法所在位置的类名或对象标识符,它将在当前类中查找,在您的情况下为Program.

你有几个选择。

  • 使GroupingA方法静态并将该行替换为List<Item> cont2 = GroupItem.GroupingA(cont1, count);

  • 创建一个 GroupItem 的实例并引用它:

    GroupItem g = new GroupItem();
    List<Item> cont2 = your.GroupingA(cont1, count);
    
  • 将方法GroupingA移出GroupItem类并将其放置在Program类中,以便您的线路知道在哪里可以找到它。
于 2013-04-18T03:02:48.110 回答