刚接触接口,重写接口成员时为什么可以引用私有成员?
例如
class UseInerface:IComparable
{
private int num;
public UseInerface(int num)
{
this.num=num;
}
public int CompareTo(Object obj) //重写CompareTo函数
{
UseInerface otherObject=obj as UseInerface;
if(otherObject==null)
throw new ArgumentException("error");
if(num<otherObject.num) //使用私有成员num
return -1;
else if(num>otherObject.num)
return 1;
else
return 0;
}
}
class Test
{
public static int Main()
{
UseInerface class1=new UseInerface(1);
UseInerface class2=new UseInerface(2);
Console.WriteLine(class1.CompareTo(2));
return(0);
}
}