读书人

小弟我从书上抄的关于接口的代码出错了

发布时间: 2012-01-18 00:23:26 作者: rapoo

我从书上抄的关于接口的代码出错了,这是为什么呢?
我从机械工业出版社出版的零基础学ASP.NET 2.0上抄了一段c#代码,如下:但是运行不正确。我的环境是win2003server,vs2005team,由于小弟刚学习.NET相关知识,请大家帮忙看看。


using System;
using System.Collections.Generic;
using System.Text;
namespace interface
{
interface ipoint
{
int x
{
get;
set;

}
int y
{
get;
set;

}
}
class mypoint : ipoint
{
private int myx;
private int myy;
public mypoint(int x, int y)
{
myx = x;
myy = y;
}
public int x
{
get { return myx; }
set { myx = Value; }
}
public int y
{
get { return myy; }
set { myy = value; }
}
}
}
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
mypoint mp = new mypoint(3, 5);
Console.Write( "我的坐标为: ");
printpoint(mp);
}
private static void printpoint(ipoint mp)
{


Console.WriteLine( "x={0},y={1} ", mp.x, mp.y);
}
}
}


[解决办法]
命名空间不一样记得先引用
[解决办法]
using System;
using System.Collections.Generic;
using System.Text;
namespace interface (interface是关键字,是不能使用的命名空间名的)
{
interface ipoint
{
int x
{
get;
set;

}
int y
{
get;
set;

}
}
class mypoint : ipoint
{
private int myx;
private int myy;
public mypoint(int x, int y)
{
myx = x;
myy = y;
}
public int x
{
get { return myx; }
set { myx = Value;(V大写了,应该小写) }
}
public int y
{
get { return myy; }
set { myy = value; }
}
}
}
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
mypoint mp = new mypoint(3, 5);
Console.Write( "我的坐标为: ");
printpoint(mp);
}
private static void printpoint(ipoint mp)
{
Console.WriteLine( "x={0},y={1} ", mp.x, mp.y);
}
}
}
[解决办法]
其实LZ,VS已经把错误给的很清楚了。你根据错误进行相应的修改就可以了

C# code
using   System; using   System.Collections.Generic; using   System.Text;[color=#FF0000]//引用interface1[/color]using interface1;[color=#FF0000]//我在这里后面多加了一个1[/color]namespace   interface1 {         interface   ipoint         {         int   x           {                 get;                 set;         }         int   y         {                 get;                 set;         }         }         class   mypoint   :   ipoint         {                 private   int   myx;                 private   int   myy;                 public   mypoint(int   x,   int   y)                 {                         myx   =   x;                         myy   =   y;                 }                 public   int   x                 {                         get   {   return   myx;   }                             //将V小写                        set   {   myx   =   value;   }                 }                 public   int   y                 {                         get   {   return   myy;   }                         set   {   myy   =   value;   }                 }         } } namespace   ConsoleApplication3 {         class   Program         {                 static   void   Main(string[]   args)                 {                         mypoint   mp   =   new   mypoint(3,   5);                         Console.Write( "我的坐标为: ");                         printpoint(mp);                 }                 private   static   void   printpoint(ipoint   mp)                 {                         Console.WriteLine( "x={0},y={1} ",   mp.x,   mp.y);                 }         } } 


[解决办法]
错误太多了
首先新建一个consoleApplication的工程
添加文件-interface文件

using System;
using System.Collections.Generic;
using System.Text;
namespace IPoint
{
public interface ipoint
{
int x
{
get;
set;

}
int y
{
get;
set;

}
}
}
然后添加一个类文件
using System;
using System.Collections.Generic;
using System.Text;
using IPoint;
public class mypoint : ipoint
{
private int myx;
private int myy;
public mypoint(int x, int y)
{
myx = x;
myy = y;
}
public int x
{
get { return myx; }
set { myx = Value; }
}
public int y
{
get { return myy; }
set { myy = value; }
}
}
然后就可以运行了。。
在program.cs里

读书人网 >C#

热点推荐