代码示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;
namespace EffectiveCsharp
{
interface IMessage
{
void Message();
}
//显式现实接口示例
class MyMessage2 : IMessage
{
IMessage Members
}
class Program
{
static void Main(string[] args)
{
MyMessage2 m = new MyMessage2();
m.Message(); //complie-time error!
m.PrintMessage(); //ok
IMessage im = m;
im.Message();
Console.ReadLine();
}
}
}
实现接口的类可以显式实现该接口的成员。当显式实现某成员时,不能通过类实例访问该成员,而只能通过该接口的实例访问该成员。using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;
namespace EffectiveCsharp
{
interface IMessage
{
void Message();
}
//显式现实接口示例
class MyMessage2 : IMessage
{
IMessage Members
}
class Program
{
static void Main(string[] args)
{
MyMessage2 m = new MyMessage2();
m.Message(); //complie-time error!
m.PrintMessage(); //ok
IMessage im = m;
im.Message();
Console.ReadLine();
}
}
}