下面的代码是在C#中,我使用的是Visual Studio 2010.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace FrontEnd
{
class Flow
{
long i;
private int x,y;
public int X
{
get;set;
}
public int Y
{
get;set;
}
private void Flow()
{
X = x;
Y = y;
}
public void NaturalNumbers(int x, int y)
{
for (i = 0; i < 9999; i++)
{
Console.WriteLine(i);
}
MessageBox.Show("done");
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译上面的代码时,我收到此错误:
错误:'Flow':成员名称不能与其封闭类型相同
为什么?我该如何解决这个问题?
msh*_*yem 113
与类名相同的方法名称称为构造函数.构造函数没有返回类型.所以正确如下:
private Flow()
{
X = x;
Y = y;
}
Run Code Online (Sandbox Code Playgroud)
或者将函数重命名为:
private void DoFlow()
{
X = x;
Y = y;
}
Run Code Online (Sandbox Code Playgroud)
虽然整个代码对我没有任何意义.
Wou*_*ort 24
问题在于方法:
private void Flow()
{
X = x;
Y = y;
}
Run Code Online (Sandbox Code Playgroud)
您的类已命名,Flow因此无法命名此方法Flow.您必须将Flow方法的名称更改为其他内容才能使此代码编译.
或者你的意思是创建一个私有构造函数来初始化你的类?如果是这种情况,则必须删除void关键字以让编译器知道您声明构造函数.
| 归档时间: |
|
| 查看次数: |
164648 次 |
| 最近记录: |