interface ISampleInterface
{
void SampleMethod();
}
interface ISampleInterface2
{
void SampleMethod();
}你这两个接口里面的函数没有声明访问权限,C#默认为是private所以在所有实现这两个接口的函数里面,都不能再声明为比private更开放的权限
{
void SampleMethod();
}
interface ISampleInterface2
{
void SampleMethod();
}你这两个接口里面的函数没有声明访问权限,C#默认为是private所以在所有实现这两个接口的函数里面,都不能再声明为比private更开放的权限
这个帖子说的很清楚,道理是一样的
说的不对。定义接口时 是无法设置访问级别的,所有的接口默认(也只能是)public的。
interface ISampleInterface
{void SampleMethod();}
class ImplementationClass : ISampleInterface
{
void ISampleInterface.SampleMethod()
{
System.Windows.Forms.MessageBox.Show("test");
}
static void Main()
{
ImplementationClass ic=new ImplementationClass();
ISampleInterface ii =ic;
ii.SampleMethod();
}
}
interface ISampleInterface
{void SampleMethod();}
class ImplementationClass : ISampleInterface
{
public virtual void SampleMethod()
{
System.Windows.Forms.MessageBox.Show("test");
}
static void Main()
{
ImplementationClass ic=new ImplementationClass();
ic.SampleMethod();
}
}
使用显示接口实现,方法不应有一个访问修饰符。用于实现一个接口的所有方法都具有public可访问性。
本书的建议尽可能显示实现接口
《visual C#2005从入门到精通》p166 显示接口的优点。
interface ISampleInterface
{
void SampleMethod();
}
interface ISampleInterface2
{
void SampleMethod();
}
//定义一个类实现接口,显式实现接口.
class ImplementationClass : ISampleInterface,ISampleInterface2
{
public void SmapleMethod(string msg)
{
System.Windows.Forms.MessageBox.Show(msg);
} void ISampleInterface.SampleMethod()
{
this.SampleMethod("test"); }
void ISampleInterface2.SampleMethod()
{
this.SampleMethod("test2"); }}
"ISampleInterface.SampleMethod()",这样不是显式接口实现吗?MSDN里面都是这样描诉的.
void ISampleInterface.SampleMethod()
{
this.SampleMethod("test"); }
使用显示接口实现来定义的一个方法不能声明为virtual,但在省略接口名之后允许。
使用显示接口实现,方法不应有一个访问修饰符。用于实现一个接口的所有方法都具有public可访问性。
本书的建议尽可能显示实现接口
《visual C#2005从入门到精通》p166 显示接口的优点。是啊,没错,现在就是不知道为什么使用显示接口实现来定义的一个方法不能声明为virtual,假如还有子类要重写这个方法,那不是做不到了吗..
你说:interface ISampleInterface
{
void SampleMethod();
}
interface ISampleInterface2
{
void SampleMethod();
}你这两个接口里面的函数没有声明访问权限,C#默认为是private所以在所有实现这两个接口的函数里面,都不能再声明为比private更开放的权限----------------------------------
接口里面的成员默认都是public的把!!
上面说错了。