新手:类的例题问题

discory 2012-03-04 01:31:52
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{

public class Assign
{
public int x;
public int y;

public void add()
{

Console.Write(x + y);
}

}

Assign Result = new Assign();

Result.x = 3; //错误 类、结构或接口成员声明中的标记“=”无效
Result.y = 3; //错误 类、结构或接口成员声明中的标记“=”无效


Result.add(); //错误 类、结构或接口成员声明中的标记“(”无效

}

}
}

这是我参照书本上自己写的在 vs2008,但是报错,大家告诉我错在哪里,为什么
...全文
43 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
discory 2012-03-04
  • 打赏
  • 举报
回复
很好,谢了
SoftLy 2012-03-04
  • 打赏
  • 举报
回复
类的定义要放在main函数的外面
namespace ConsoleApplication1
{
public class Assign
{
public int x;
public int y;

public void add()
{

Console.Write(x + y);
}

}

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

Assign Result = new Assign();

Result.x = 3; //错误 类、结构或接口成员声明中的标记“=”无效
Result.y = 3; //错误 类、结构或接口成员声明中的标记“=”无效


Result.add(); //错误 类、结构或接口成员声明中的标记“(”无效

}

}
}
这样应该可以了。。。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧