c#中的接口
interface
[public|protected|private] interface interfacename
{
//mothed
// propery
// event
//delegate
}
在实现接口时,带接口名与不带接口的区别
不带的区别eg:
public interface imyshow
{
void show();
}
public class myshow:imyshow
{
public void show()//必须写上前的public若写成void show()出错
{
system.console.write(" 不带接口名");
}
}
public class mymain
{
public static void main()
{
// 用类定义引用
myshow obj=new myshow();
obj.show();
//用接口引用方法
imyshow obj2=new myshow();
obj2.show();
}
}
//带接口名
public interface imyshow
{
system.console.write("带接口名");
}
public class myshow:imyshow
{
void imyshow.show()// 前面不能带上任何限定词
{
system.console.write("带接口名");
}
}
public class mymain
{
public static void main()
{
myshow obj=new myshow();
obj.show();//非法因为加了限定词后,这个方法专属于专们的一个引用,只能有接口去引用
imyshow obj2=new myshow();
obj2.show();
}
}
看完上面的内容我想为c#的爱好留个问题。请大家一起来讨论一下
public interface imyshow
{
void show();
}
public interface imyshow2
{
void show();
}
public class myclass:imyshow,imyshow2
{
public myclass()
{
}
void imyshow.show()
{
system.console.write("imyshow");
}
public void show()
{
system.console.write("myclass show");
}
void imyshow2.show()
{
system.console.write("imyshow2.show()");
// todo: 添加 myclass.show 实现
}
}
class class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main(string[] args)
{
imyshow obj2=new myclass ();
obj2.show();
imyshow obj1=new myclass();
obj1.show();
myclass obj=new myclass();
obj.show();
}
}
}
namespace wfgspace
public interface imyshow
sub show()
function add(byval a as integer, byval b as integer) as integer
end interface
public interface imyshow2
sub show()
function add(byval a as integer, byval b as integer) as integer
end interface
public class mycls : implements imyshow, imyshow2
private ivalue as integer
sub show() implements imyshow.show
system.console.write("wfng fu guo")
end sub
sub show2() implements imyshow2.show
system.console.write("wfg")
end sub
function add2(byval a as integer, byval b as integer) as integer implements imyshow.add, imyshow2.add
ivalue = a + b
system.console.writeline("{0}+{1}={2}", a, b, ivalue)
end function
end class
public class common
public shared sub main()
dim obj as mycls = new mycls
dim obj2 as imyshow = new mycls
dim obj3 as imyshow2 = new mycls
system.console.writeline("class mycls object")
obj.show2()
obj.add2(5, 4)
system.console.writeline("interface imyshow object")
obj2.show()
obj2.add(5, 4)
system.console.writeline("interface imyshow2 object")
obj3.show()
obj3.add(5, 4)
end sub
end class
end namespace
新闻热点
疑难解答
图片精选