用下面的代码定义了一个类,编译时报错
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: 命名空间并不直接包含诸如字段或方法之类的成员
我是菜鸟,请高手不惜赐教

解决方案 »

  1.   

    public Point3D(int x, int y, int z): Point(x, y)
    {
        this.z = z;
    }改为
    public Point3D(int x, int y, int z): base(x, y)
    {
        this.z = z;
    }
      

  2.   

    .....教材上都说有Point(x, y);
      

  3.   

    System.Drawing.Point是结构,不是类。你这里的Point是自己定义的类。
      

  4.   

    楼主不要过于执着。
    其实你的问题就是构造函数之间的调用问题

    public MyClass(): this(0)
    {
    }
    public MyClass(int x)
    {
        this.x = x;
    }
    这样的方式,this代表调用该类中其它的构造函数。public MyBaseClass(int x)
    {
        this.x = x;
    }public MyClass(): base(0)
    {
    }base代表调用基类的构造函数。
      

  5.   

    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): 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);
    }
    }
    }
      

  6.   

    明白了,谢谢sunjian_qi(sonne)  也谢谢weiljj(潛客) 的参与