用下面的代码定义了一个类,编译时报错
using System;namespace test
{
public class Point
{
public int x, y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
public class Point3D: Point
{
public int z;
public Point3D(int x, int y, int z): Point(x, y){
this.z = z;
}
}
static void Main(){
Point a = new Point(10, 20);
Point3D b = new Point3D(100, 200, 300);
Console.WriteLine("Point:{0}, {0}\n", a.x, a.y);
Console.WriteLine("Point3D:{0}, {0}, {0}", b.x, b.y, b.z);
}
}
-------------------
错误信息:
F:\Solution\CSharp学习过程\Chapter 1.6.3>csc test.cs
Microsoft (R) Visual C# .NET 编译器版本 7.10.3052.4
用于 Microsoft (R) .NET Framework 版本 1.1.4322
版权所有 (C) Microsoft Corporation 2001-2002。保留所有权利。test.cs(17,40): error CS1018: 应输入关键字 this 或 base
test.cs(17,47): error CS1001: 应输入标识符
test.cs(17,50): error CS1001: 应输入标识符
test.cs(21,2): error CS0116: 命名空间并不直接包含诸如字段或方法之类的成员
我是菜鸟,请高手不惜赐教
using System;namespace test
{
public class Point
{
public int x, y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
public class Point3D: Point
{
public int z;
public Point3D(int x, int y, int z): Point(x, y){
this.z = z;
}
}
static void Main(){
Point a = new Point(10, 20);
Point3D b = new Point3D(100, 200, 300);
Console.WriteLine("Point:{0}, {0}\n", a.x, a.y);
Console.WriteLine("Point3D:{0}, {0}, {0}", b.x, b.y, b.z);
}
}
-------------------
错误信息:
F:\Solution\CSharp学习过程\Chapter 1.6.3>csc test.cs
Microsoft (R) Visual C# .NET 编译器版本 7.10.3052.4
用于 Microsoft (R) .NET Framework 版本 1.1.4322
版权所有 (C) Microsoft Corporation 2001-2002。保留所有权利。test.cs(17,40): error CS1018: 应输入关键字 this 或 base
test.cs(17,47): error CS1001: 应输入标识符
test.cs(17,50): error CS1001: 应输入标识符
test.cs(21,2): error CS0116: 命名空间并不直接包含诸如字段或方法之类的成员
我是菜鸟,请高手不惜赐教
解决方案 »
- (请教)VS2008左边除了[工具箱],[服务器资源管理器]选项卡外,[数据源]的选项卡在哪找?
- HttpWebRequest post数据到java接口
- 如何制造安装程序?
- c# 调用vc的DLL问题
- 请问如何做想qq 那样的 截屏的 控件!
- 怎么机器总自动改我的代码啊???
- 窗口问题
- 请教问题 Exception Processing ErrorPage[errorCode=404, location=/404.html]
- 新手问题
- 能启动一个 线程 执行代参数的 函数 吗?
- 点击一个控件,再点击随意一点,使控件移到该点
- AxMSFlexGridLib在.net工具箱里能不能找到这个控件啊,它是什么控件???谢谢
{
this.z = z;
}改为
public Point3D(int x, int y, int z): base(x, y)
{
this.z = z;
}
其实你的问题就是构造函数之间的调用问题
用
public MyClass(): this(0)
{
}
public MyClass(int x)
{
this.x = x;
}
这样的方式,this代表调用该类中其它的构造函数。public MyBaseClass(int x)
{
this.x = x;
}public MyClass(): base(0)
{
}base代表调用基类的构造函数。
{
public class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
public class Point3D: Point
{
public int z;
public Point3D(int x, int y, int z): base(x, y)
{
this.z = z;
}
static void Main()
{
Point a = new Point(10, 20);
Point3D b = new Point3D(100, 200, 300);
Console.WriteLine("Point:{0}, {0}\n", a.x, a.y);
Console.WriteLine("Point3D:{0}, {0}, {0}", b.x, b.y, b.z);
}
}
}