C#基础概念之接口的多继承会带来哪些问题?

创新互联公司-专业网站定制、快速模板网站建设、高性价比集宁网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式集宁网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖集宁地区。费用合理售后完善,十多年实体公司更值得信赖。
C# 中的接口与类不同,可以使用多继承,即一个子接口可以有多个父接口。但如果两个父成员具有同名的成员,就产生了二义性(这也正是 C# 中类取消了多继承的原因之一),这时在实现时最好使用显式的声明
示例:
- using System;
 - using System.Collections.Generic;
 - using System.Text;
 - namespace Example17 {
 - class Program {
 - //一个完整的接口声明示例interface IExample {
 - //属性string P { get;set;
 - }
 - //方法string F(int Value);
 - //事件event EventHandler E;
 - //索引指示器string this[int Index] {
 - get;set;
 - }
 - interface IA {
 - int Count {
 - get; set;
 - }
 - }
 - interface IB {
 - int Count();
 - }
 - //IC接口从IA和IB多重继承interface IC : IA, IB {
 - }
 - class C : IC {
 - private int count = 100;
 - //显式声明实现IA接口中的Count属性int IA.Count {
 - get {
 - return 100;
 - }
 - set { count = value; }
 - }
 - //显式声明实现IB接口中的Count方法int IB.Count(){
 - return count * count;} static void Main(string[] args){
 - C tmpObj = new C();
 - //调用时也要显式转换Console.WriteLine("Count property: {0}",((IA)tmpObj)。Count);
 - Console.WriteLine("Count function: {0}",((IB)tmpObj)。Count());
 - Console.ReadLine();
 - }
 
C#基础概念之抽象类和接口的区别?
抽象类(abstract class)可以包含功能定义和实现,接口(interface)只能包含功能定义,抽象类是从一系列相关对象中抽象出来的概念, 因此反映的是事物的内部共性;接口是为了满足外部调用而定义的一个功能约定, 因此反映的是事物的外部特性,分析对象,提炼内部共性形成抽象类,用以表示对象本质,即“是什么”,为外部提供调用或功能需要扩充时优先使用接口
C#基础概念之别名指示符是什么?
通过别名指示符我们可以为某个类型起一个别名,主要用于解决两个命名空间内有同名类型的冲突或避免使用冗余的命名空间,别名指示符只在一个单元文件内起作用
示例:
- using System;
 - using System.Collections.Generic;
 - using System.Text;
 - namespace com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib01 {
 - class Class1 {
 - public override string ToString(){
 - return "com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib01's Class1";
 - }
 - Class2.cs
 - using System;
 - using System.Collections.Generic;
 - using System.Text;
 - namespace com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib02 {
 - class Class1 {
 - public override string ToString(){
 - return "com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib02's Class1";
 - }主单元(Program.cs):
 - using System;
 - using System.Collections.Generic;
 - using System.Text;
 - //使用别名指示符解决同名类型的冲突
 - using Lib01Class1 = com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib01.Class1;
 - using Lib02Class2 = com.nblogs.reonlyrun.CSharp26QExample.Example19.Lib02.Class1;
 - namespace Example19 {
 - class Program { static void Main(string[] args){
 - Lib01Class1 tmpObj1 = new Lib01Class1();
 - Lib02Class2 tmpObj2 = new Lib02Class2();
 - Console.WriteLine(tmpObj1);
 - Console.WriteLine(tmpObj2);
 - Console.ReadLine();
 - }
 
                网站栏目:C#基础概念总结
                
                URL网址:http://www.csdahua.cn/qtweb/news13/169863.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网