17.接口的多继承会带来哪些问题?
答:


C# 中的接口与类不同,可以使用多继承,即一个子接口可以有多个父接口。但如果两个父成员具有同名的成员,就产生了二义性(这也正是 C# 中类取消了多继承的原因之一),这时在实现时最好使用显式的声明

示例:

 

Code
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();
}
}
}

 

 

结果:
Count property: 100
Count function: 10000 

 

18.抽象类和接口的区别?
答:


抽象类(abstract class)可以包含功能定义和实现,接口(interface)只能包含功能定义

抽象类是从一系列相关对象中抽象出来的概念, 因此反映的是事物的内部共性;接口是为了满足外部调用而定义的一个功能约定, 因此反映的是事物的外部特性

分析对象,提炼内部共性形成抽象类,用以表示对象本质,即“是什么”

为外部提供调用或功能需要扩充时优先使用接口

posted on 2009-03-20 10:55  ★偏翩☆逍遥  阅读(427)  评论(0编辑  收藏  举报