欢迎来到天天文库
浏览记录
ID:19914060
大小:29.50 KB
页数:3页
时间:2018-10-07
《c#显式调用与隐式调用区别》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、namespaceMyInface{//显示接口就是只能用接口调用。//隐式接口和类都能调用。publicinterfaceIBookList{voidAdd(stringBookName);voidAppend(stringBookName);voidRemove(intposition);intCount{get;}stringthis[intindex]{get;set;}}//隐式调用publicclassBookList:IBookList{privateListbook
2、list=newList();#regionIBookList成员publicvoidAdd(stringBookName){booklist.Add(BookName);}publicvoidAppend(stringBookName){booklist.Insert(booklist.Count,BookName);}publicvoidRemove(intposition){booklist.RemoveAt(position);}publicintCount{get{ret
3、urnbooklist.Count;}}publicstringthis[intindex]{get{returnbooklist[index];}set{booklist[index]=value;}}#endregion}//显示调用该类只能用接口调用。publicclassBookList2:IBookList{privateListbooklist=newList();#regionIBookList成员voidIBookList.Add(stringBoo
4、kName){booklist.Add(BookName);}voidIBookList.Append(stringBookName){booklist.Insert(booklist.Count,BookName);}voidIBookList.Remove(intposition){booklist.Remove(position.ToString());}intIBookList.Count{get{returnbooklist.Count;}}stringIBookList.this[in
5、tindex]{get{returnbooklist[index];}set{booklist[index]=value;}}#endregion}classtext{staticvoidMain(){//隐式调用。BookListbklist=newBookList();bklist.Add("¥¥¥隐式");Console.Write("实现隐式接口的例子:使用类:{0}",bklist[0]);IBookListibklist=(IBookList)bklist;//转变类型后,接连联内容也
6、一同赋值给过来了。ibklist.Add("$$$$$隐式");//这里索引是第二个了。Console.Write("实现隐式接口的例子:使用接口{0}",ibklist[1]);//显示调用只能用接口调用。BookList2bklist2=newBookList2();//如果直接bklist2.Add("显示");这样就出错。如://只能如下:IBookListIbk=(IBookList)bklist2;Ibk.Add("$$$$显示接口");//用接口增加Console.Write("实现
7、显示接口的例子:使用接口{0}",Ibk[0]);//用接口调用。Console.ReadLine();}}}
此文档下载收益归作者所有