
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
	<head><meta forua="true" http-equiv="Cache-Control" content="max-age=0" /></head><card id="MainCard" title="&#x6B22;&#x8FCE;&#x5149;&#x4E34;"><p><a href="wap.asp">&#x6728;&#x5B50;&#x5C4B;</a><br/>&nbsp;</p><p><b>&#x6807;&#x9898;&#x3A;</b> &#x45;&#x66;&#x66;&#x65;&#x63;&#x74;&#x69;&#x76;&#x65;&#x20;&#x43;&#x23;&#x20;&#x539F;&#x5219;&#x32;&#x30;&#xFF1A;&#x660E;&#x8FA8;&#x63A5;&#x53E3;&#x5B9E;&#x73B0;&#x548C;&#x865A;&#x51FD;&#x6570;&#x91CD;&#x8F7D;&#x7684;&#x533A;&#x522B;</p><p><b>&#x4F5C;&#x8005;&#x3A;</b> &#x64;&#x6E;&#x61;&#x77;&#x6F;</p><p><b>&#x65E5;&#x671F;&#x3A;</b> &#x32;&#x30;&#x30;&#x39;&#x2D;&#x30;&#x38;&#x2D;&#x30;&#x35;&#x20;&#x30;&#x39;&#x3A;&#x33;&#x33;&#x20;&#x41;&#x4D;</p><p><b>&#x5206;&#x7C7B;&#x3A;</b> <a href="wap.asp?do=showLog&amp;cateID=5">&#x57;&#x65;&#x62;&#x7F16;&#x7A0B;</a></p><p><b>&#x5185;&#x5BB9;&#x3A;</b> &#x7C97;&#x7565;&#x7684;&#x770B;&#x4E00;&#x4E0B;&#xFF0C;&#x611F;&#x89C9;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x548C;&#x865A;&#x51FD;&#x6570;&#x91CD;&#x8F7D;&#x662F;&#x4E00;&#x6837;&#x7684;&#x3002;&#x4F60;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E9B;&#x5BF9;&#x8C61;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x4E9B;&#x5BF9;&#x8C61;&#x662F;&#x5728;&#x53E6;&#x4E00;&#x4E2A;&#x7C7B;&#x578B;&#x91CC;&#x7533;&#x660E;&#x7684;&#x3002;&#x4F60;&#x88AB;&#x7B2C;&#x4E00;&#x611F;&#x89C9;&#x9A97;&#x4E86;&#xFF0C;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x4E0E;&#x865A;&#x51FD;&#x6570;&#x91CD;&#x8F7D;&#x662F;&#x5B8C;&#x5168;&#x4E0D;&#x540C;&#x7684;&#x3002;&#x5728;&#x63A5;&#x53E3;&#x91CC;&#x5B9A;&#x4E49;&#x7684;&#x6210;&#x5458;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x662F;&#x6839;&#x672C;&#x4E0D;&#x5B58;&#x5728;&#x5B9E;&#x9645;&#x5185;&#x5BB9;&#x7684;&#x3002;<br/>&#x6D3E;&#x751F;&#x7C7B;&#x4E0D;&#x80FD;&#x91CD;&#x8F7D;&#x57FA;&#x7C7B;&#x4E2D;&#x7684;&#x63A5;&#x53E3;&#x6210;&#x5458;&#x3002;&#x63A5;&#x53E3;&#x53EF;&#x4EE5;&#x9690;&#x5F0F;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x5C31;&#x662F;&#x628A;&#x5B83;&#x4EEC;&#x4ECE;&#x7C7B;&#x7684;&#x516C;&#x5171;&#x63A5;&#x53E3;&#x4E2D;&#x9690;&#x85CF;&#x3002;&#x5B83;&#x4EEC;&#x7684;&#x6982;&#x5FF5;&#x662F;&#x4E0D;&#x540C;&#x7684;&#x800C;&#x4E14;&#x4F7F;&#x7528;&#x4E5F;&#x662F;&#x4E0D;&#x540C;&#x7684;&#x3002;<br/>&#x4F46;&#x4F60;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x7684;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#xFF1A;&#x8BA9;&#x4F60;&#x7684;&#x6D3E;&#x751F;&#x7C7B;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x4F60;&#x7684;&#x5B9E;&#x73B0;&#x3002;&#x4F60;&#x53EA;&#x7528;&#x5BF9;&#x6D3E;&#x751F;&#x7C7B;&#x505A;&#x4E00;&#x4E2A;Hook&#x5C31;&#x884C;&#x4E86;&#x3002;(&#x8BD1;&#x6CE8;&#xFF1A;&#x76F8;&#x4FE1;&#x5199;&#x8FC7;C++&#x7A0B;&#x5E8F;&#x7684;&#x4EBA;&#x5C31;&#x77E5;&#x9053;hook&#x662F;&#x4EC0;&#x4E48;&#x610F;&#x601D;&#xFF0C;&#x800C;&#x4E14;&#x6211;&#x4E5F;&#x5B9E;&#x5728;&#x60F3;&#x4E0D;&#x5230;&#x628A;hook&#x8BD1;&#x6210;&#x4EC0;&#x4E48;&#x6BD4;&#x8F83;&#x597D;&#xFF0C;&#x6240;&#x4EE5;&#x5C31;&#x76F4;&#x63A5;&#x7528;hook&#x8FD9;&#x4E2A;&#x539F;&#x8BCD;&#x4E86;&#xFF0C;&#x5C31;&#x50CF;bug&#x4E00;&#x6837;&#x3002;)<br/>&#x4E3A;&#x4E86;&#x5C55;&#x793A;&#x5B83;&#x4EEC;&#x7684;&#x4E0D;&#x540C;&#x4E4B;&#x5904;&#xFF0C;&#x8BD5;&#x7740;&#x505A;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x63A5;&#x53E3;&#x4EE5;&#x53CA;&#x5728;&#x4E00;&#x4E2A;&#x7C7B;&#x4E2D;&#x5B9E;&#x73B0;&#x5B83;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;interface IMsg<br/>{<br/>&nbsp;&nbsp;void Message();<br/>}<br/>public class MyClass : IMsg<br/>{<br/>&nbsp;&nbsp;public void Message()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( &#34;MyClass&#34; );<br/>&nbsp;&nbsp;}<br/>}<br/>Message()&#x65B9;&#x6CD5;&#x662F;MyClass&#x7684;&#x516C;&#x5171;&#x63A5;&#x53E3;&#xFF0C;Message&#x540C;&#x6837;&#x53EF;&#x4EE5;&#x7528;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#x6307;&#x9488;IMsg&#x6765;&#x8BBF;&#x95EE;&#x3002;&#x73B0;&#x5728;&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x4E00;&#x70B9;&#x7E41;&#x6742;&#x7684;&#xFF0C;&#x6DFB;&#x52A0;&#x4E00;&#x4E2A;&#x6D3E;&#x751F;&#x7C7B;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class MyDerivedClass : MyClass<br/>{<br/>&nbsp;&nbsp;public new void Message()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( &#34;MyDerivedClass&#34; );<br/>&nbsp;&nbsp;}<br/>}<br/>&#x6CE8;&#x610F;&#x5230;&#xFF0C;&#x6211;&#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x5173;&#x952E;&#x5B57;new&#x5728;Message&#x65B9;&#x6CD5;&#x4E0A;&#xFF0C;&#x7528;&#x4E8E;&#x533A;&#x522B;&#x524D;&#x9762;&#x7684;&#x4E00;&#x4E2A;Message(&#x53C2;&#x89C1;&#x539F;&#x5219;29)&#x3002;MyClass.Message()&#x4E0D;&#x662F;&#x865A;&#x51FD;&#x6570;&#xFF0C;&#x6D3E;&#x751F;&#x7C7B;&#x53EF;&#x4EE5;&#x4E0D;&#x63D0;&#x4F9B;&#x91CD;&#x8F7D;&#x7248;&#x672C;&#x3002;MyDerived&#x7C7B;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;&#x65B0;&#x7684;Message&#x65B9;&#x6CD5;&#xFF0C;&#x4F46;&#x8FD9;&#x4E2A;&#x65B9;&#x6CD5;&#x5E76;&#x4E0D;&#x662F;&#x91CD;&#x8F7D;MyClass.Message:&#x5B83;&#x9690;&#x85CF;&#x4E86;&#x539F;&#x6765;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x800C;&#x4E14;&#xFF0C;MyClass.Message&#x8FD8;&#x662F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;IMsg&#x7684;&#x5F15;&#x7528;&#x6765;&#x8BBF;&#x95EE;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;MyDerivedClass d = new MyDerivedClass( );<br/>d.Message( ); // prints &#34;MyDerivedClass&#34;.<br/>IMsg m = d as IMsg;<br/>m.Message( ); // prints &#34;MyClass&#34;<br/>&#x63A5;&#x53E3;&#x65B9;&#x6CD5;&#x4E0D;&#x662F;&#x865A;&#x7684;&#xFF0C;&#x5F53;&#x4F60;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#x65F6;&#xFF0C;&#x4F60;&#x5C31;&#x8981;&#x5728;&#x8BE6;&#x7EC6;&#x7684;&#x76F8;&#x5173;&#x7C7B;&#x578B;&#x4E2D;&#x7533;&#x660E;&#x5177;&#x4F53;&#x7684;&#x5B9E;&#x73B0;&#x5185;&#x5BB9;&#x3002;<br/>&#x4F46;&#x4F60;&#x53EF;&#x80FD;&#x60F3;&#x8981;&#x521B;&#x5EFA;&#x63A5;&#x53E3;&#xFF0C;&#x5728;&#x57FA;&#x7C7B;&#x4E2D;&#x5B9E;&#x73B0;&#x8FD9;&#x4E9B;&#x63A5;&#x53E3;&#x800C;&#x4E14;&#x5728;&#x6D3E;&#x751F;&#x7C7B;&#x4E2D;&#x4FEE;&#x6539;&#x5B83;&#x4EEC;&#x7684;&#x884C;&#x4E3A;&#x3002;&#x8FD9;&#x662F;&#x53EF;&#x4EE5;&#x529E;&#x6CD5;&#x5230;&#x7684;&#x3002;&#x4F60;&#x6709;&#x4E24;&#x4E2A;&#x9009;&#x62E9;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x8BBF;&#x95EE;&#x57FA;&#x7C7B;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x6D3E;&#x751F;&#x7C7B;&#x4E2D;&#x91CD;&#x65B0;&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class MyDerivedClass : MyClass, IMsg<br/>{<br/>&nbsp;&nbsp;public new void Message()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( &#34;MyDerivedClass&#34; );<br/>&nbsp;&nbsp;}<br/>}<br/>&#x6DFB;&#x52A0;&#x7684;IMsg&#x8BA9;&#x4F60;&#x7684;&#x6D3E;&#x751F;&#x7C7B;&#x7684;&#x884C;&#x4E3A;&#x53D1;&#x751F;&#x4E86;&#x6539;&#x53D8;&#xFF0C;&#x4EE5;&#x81F3;IMsg.Message&#x73B0;&#x5728;&#x662F;&#x5728;&#x6D3E;&#x751F;&#x7C7B;&#x4E0A;&#x4F7F;&#x7528;&#x7684;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;MyDerivedClass d = new MyDerivedClass( );<br/>d.Message( ); // prints &#34;MyDerivedClass&#34;.<br/>IMsg m = d as IMsg;<br/>m.Message( ); // prints &#34;MyDerivedClass&#34;<br/>&#x6D3E;&#x751F;&#x7C7B;&#x4E0A;&#x8FD8;&#x662F;&#x987B;&#x8981;&#x5728;MyDerivedClass.Message()&#x65B9;&#x6CD5;&#x4E0A;&#x6DFB;&#x52A0;&#x5173;&#x952E;&#x5B57;new&#xFF0C;&#x8FD9;&#x8FD8;&#x662F;&#x6709;&#x4E00;&#x70B9;&#x9690;&#x60A3;(&#x53C2;&#x89C1;&#x539F;&#x5219;29)&#x3002;&#x57FA;&#x7C7B;&#x8FD8;&#x662F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x63A5;&#x53E3;&#x5F15;&#x7528;&#x6765;&#x8BBF;&#x95EE;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;MyDerivedClass d = new MyDerivedClass( );<br/>d.Message( ); // prints &#34;MyDerivedClass&#34;.<br/>IMsg m = d as IMsg;<br/>m.Message( ); // prints &#34;MyDerivedClass&#34;<br/>MyClass b = d;<br/>b.Message( ); // prints &#34;MyClass&#34;<br/>&#x552F;&#x4E00;&#x53EF;&#x4EE5;&#x4FEE;&#x6B63;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#x7684;&#x65B9;&#x6CD5;&#x662F;&#x4FEE;&#x6539;&#x57FA;&#x7C7B;&#xFF0C;&#x628A;&#x63A5;&#x53E3;&#x7684;&#x7533;&#x660E;&#x4FEE;&#x6539;&#x4E3A;&#x865A;&#x51FD;&#x6570;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class MyClass : IMsg<br/>{<br/>&nbsp;&nbsp;public virtual void Message()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( &#34;MyClass&#34; );<br/>&nbsp;&nbsp;}<br/>}<br/>public class MyDerivedClass : MyClass<br/>{<br/>&nbsp;&nbsp;public override void Message()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( &#34;MyDerivedClass&#34; );<br/>&nbsp;&nbsp;}<br/>}<br/>MyDerivedClass&#x4EE5;&#x53CA;&#x5176;&#x5B83;&#x6240;&#x6709;&#x4ECE;MyClass&#x6D3E;&#x751F;&#x7684;&#x7C7B;&#x53EF;&#x4EE5;&#x7533;&#x660E;&#x5B83;&#x4EEC;&#x81EA;&#x5DF1;&#x7684;Message()&#x65B9;&#x6CD5;&#x3002;&#x8FD9;&#x4E2A;&#x91CD;&#x8F7D;&#x7684;&#x7248;&#x672C;&#x6BCF;&#x6B21;&#x90FD;&#x4F1A;&#x8C03;&#x7528;&#xFF1A;&#x901A;&#x8FC7;MyDerivedClass&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x901A;&#x8FC7;IMsg&#x63A5;&#x53E3;&#x7684;&#x5F15;&#x7528;&#xFF0C;&#x6216;&#x8005;&#x76F4;&#x63A5;&#x901A;&#x8FC7;MyClass&#x7684;&#x5F15;&#x7528;&#x3002;<br/>&#x5982;&#x679C;&#x4F60;&#x4E0D;&#x559C;&#x6B22;&#x6DF7;&#x6742;&#x7684;&#x865A;&#x51FD;&#x6570;&#x6982;&#x5FF5;&#xFF0C;&#x90A3;&#x5C31;&#x5BF9;MyClass&#x7684;&#x5B9A;&#x4E49;&#x505A;&#x4E00;&#x4E2A;&#x5C0F;&#x7684;&#x4FEE;&#x6539;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public abstract class MyClass, IMsg<br/>{<br/>&nbsp;&nbsp;public abstract void Message();<br/>}<br/>&#x662F;&#x7684;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x7528;&#x4E00;&#x4E2A;&#x62BD;&#x8C61;&#x65B9;&#x6CD5;&#x6765;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#x3002;&#x901A;&#x8FC7;&#x7533;&#x660E;&#x4E00;&#x4E2A;&#x63A5;&#x53E3;&#x5185;&#x7684;&#x62BD;&#x8C61;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x8BA9;&#x4F60;&#x7684;&#x6240;&#x6709;&#x6D3E;&#x751F;&#x90FD;&#x5FC5;&#x987B;&#x5B9E;&#x73B0;&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x3002;&#x73B0;&#x5728;&#xFF0C;IMsg&#x63A5;&#x53E3;&#x6210;&#x4E3A;&#x4E86;MyClass&#x7684;&#x4E00;&#x4E2A;&#x7EC4;&#x6210;&#x90E8;&#x4EFD;&#xFF0C;&#x4F60;&#x7684;&#x6BCF;&#x4E00;&#x4E2A;&#x6D3E;&#x751F;&#x7C7B;&#x90FD;&#x5FC5;&#x987B;&#x5B9E;&#x73B0;&#x5B83;&#x3002;<br/>&#x9690;&#x5F0F;&#x63A5;&#x53E3;&#x5B9E;&#x73B0;&#xFF0C;&#x53EF;&#x4EE5;&#x8BA9;&#x4F60;&#x5728;&#x4E00;&#x4E2A;&#x7C7B;&#x4E0A;&#x9690;&#x85CF;&#x516C;&#x5171;&#x7684;&#x63A5;&#x53E3;&#x6210;&#x5458;&#x65B9;&#x6CD5;&#xFF0C;&#x800C;&#x4E14;&#x4E5F;&#x5B9E;&#x73B0;&#x4E86;&#x8FD9;&#x4E2A;&#x63A5;&#x53E3;&#x3002;&#x5B83;&#x5728;&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x548C;&#x865A;&#x51FD;&#x6570;&#x91CD;&#x8F7D;&#x4E0A;&#x7ED5;&#x4E86;&#x51E0;&#x4E2A;&#x5708;&#x3002;&#x5F53;&#x6709;&#x591A;&#x4E2A;&#x5408;&#x9002;&#x7684;&#x51FD;&#x6570;&#x7248;&#x672C;&#x65F6;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5229;&#x7528;&#x9690;&#x5F0F;&#x63A5;&#x53E3;&#x7684;&#x5B9E;&#x73B0;&#x6765;&#x9650;&#x5236;&#x7528;&#x6237;&#x7684;&#x7F16;&#x7801;&#x3002;&#x5728;&#x539F;&#x5219;26&#x8BB2;&#x5230;&#x7684;IComparable&#x4E60;&#x60EF;&#x4F1A;&#x8BE6;&#x7EC6;&#x7684;&#x8BA8;&#x8BBA;&#x8FD9;&#x4E00;&#x70B9;&#x3002;<br/>&#x5B9E;&#x73B0;&#x63A5;&#x53E3;&#x8BA9;&#x6211;&#x4EEC;&#x6709;&#x66F4;&#x591A;&#x7684;&#x9009;&#x62E9;&#xFF0C;&#x7528;&#x4E8E;&#x521B;&#x5EFA;&#x548C;&#x91CD;&#x8F7D;&#x865A;&#x51FD;&#x6570;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x521B;&#x5EFA;&#x9690;&#x79D8;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x865A;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x6216;&#x8005;&#x62BD;&#x8C61;&#x5173;&#x8054;&#x5230;&#x6D3E;&#x751F;&#x7C7B;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x7CBE;&#x786E;&#x7684;&#x51B3;&#x5B9A;&#xFF0C;&#x4F60;&#x7684;&#x6D3E;&#x751F;&#x7C7B;&#x5982;&#x4F55;&#x4EE5;&#x53CA;&#x4F55;&#x65F6;&#xFF0C;&#x4FEE;&#x6539;&#x63A5;&#x53E3;&#x7684;&#x9ED8;&#x8BA4;&#x5B9E;&#x73B0;&#x3002;&#x63A5;&#x53E3;&#x65B9;&#x6CD5;&#x4E0D;&#x662F;&#x865A;&#x65B9;&#x6CD5;&#xFF0C;&#x800C;&#x662F;&#x4E00;&#x4E2A;&#x72EC;&#x7ACB;&#x7684;&#x7EA6;&#x5B9A;!<br/>========================================================&nbsp;&nbsp;&nbsp;&nbsp;<br/>Item 20: Distinguish Between Implementing Interfaces and Overriding Virtual Functions<br/>At first glance, implementing an interface seems to be the same as overriding avirtual function. You provide a definition for a member that has been declared in another type. That first glance is very deceiving. Implementing an interface is very different from overriding a virtual function. Members declared in interfaces are not virtualat least, not by default.<br/>Derived classes cannot override an interface member implemented in a base class. Interfaces can be explicitly implemented, which hides them from a class&#39;s public interface. They are different concepts with different uses.<br/>But you can implement interfaces in such a manner that derived classes can modify your implementation. You just have to cr&#101;ate hooks for derived classes.<br/>To illustrate the differences, examine a simple interface and implementation of it in one class:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;interface IMsg<br/>{<br/>&nbsp;&nbsp;void Message();<br/>}<br/>public class MyClass : IMsg<br/>{<br/>&nbsp;&nbsp;public void Message()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( &#34;MyClass&#34; );<br/>&nbsp;&nbsp;}<br/>}<br/>The Message() method is part of MyClass&#39;s public interface. Message can also be accessed through the IMsg point that is part of the MyClass type. Now let&#39;s complicate the situation a little by adding a derived class:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class MyDerivedClass : MyClass<br/>{<br/>&nbsp;&nbsp;public new void Message()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( &#34;MyDerivedClass&#34; );<br/>&nbsp;&nbsp;}<br/>}<br/>Notice that I had to add the new keyword to the definition of the previous Message method (see Item 29). MyClass.Message() is not virtual. Derived classes cannot provide an overridden version of Message. The MyDerived class cr&#101;ates anew Message method, but that method does not override MyClass.Message: It hides it. Furthermore, MyClass. Message is still available through the IMsg reference:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;MyDerivedClass d = new MyDerivedClass( );<br/>d.Message( ); // prints &#34;MyDerivedClass&#34;.<br/>IMsg m = d as IMsg;<br/>m.Message( ); // prints &#34;MyClass&#34;<br/>Interface methods are not virtual. When you implement an interface, you are declaring a concrete implementation of a particular contract in that type.<br/>But you often want to cr&#101;ate interfaces, implement them in base classes, and modify the behavior in derived classes. You can. You&#39;ve got two options. If you do not have access to the base class, you can reimplement the interface in the derived class:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class MyDerivedClass : MyClass, IMsg<br/>{<br/>&nbsp;&nbsp;public new void Message()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( &#34;MyDerivedClass&#34; );<br/>&nbsp;&nbsp;}<br/>}<br/>The addition of the IMsg keyword changes the behavior of your derived class so that IMsg.Message() now uses the derived class version:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;MyDerivedClass d = new MyDerivedClass( );<br/>d.Message( ); // prints &#34;MyDerivedClass&#34;.<br/>IMsg m = d as IMsg;<br/>m.Message( ); // prints &#34;MyDerivedClass&#34;<br/>You still need the new keyword on the MyDerivedClass.Message() method. That&#39;s your clue that there are still problems (see Item 29). The base class version is still accessible through a reference to the base class:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;MyDerivedClass d = new MyDerivedClass( );<br/>d.Message( ); // prints &#34;MyDerivedClass&#34;.<br/>IMsg m = d as IMsg;<br/>m.Message( ); // prints &#34;MyDerivedClass&#34;<br/>MyClass b = d;<br/>b.Message( ); // prints &#34;MyClass&#34;<br/>The only way to fix this problem is to modify the base class, declaring that the interface methods should be virtual:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class MyClass : IMsg<br/>{<br/>&nbsp;&nbsp;public virtual void Message()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( &#34;MyClass&#34; );<br/>&nbsp;&nbsp;}<br/>}<br/>public class MyDerivedClass : MyClass<br/>{<br/>&nbsp;&nbsp;public override void Message()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( &#34;MyDerivedClass&#34; );<br/>&nbsp;&nbsp;}<br/>}<br/>MyDerivedClassand all classes derived from MyClasscan declare their own methods for Message(). The overridden version will be called every time: through the MyDerivedClass reference, through the IMsg reference, and through the MyClass reference.<br/>If you dislike the concept of impure virtual functions, just make one small change to the definition of MyClass:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public abstract class MyClass, IMsg<br/>{<br/>&nbsp;&nbsp;public abstract void Message();<br/>}<br/>Yes, you can implement an interface without actually implementing the methods in that interface. By declaring abstract versions of the methods in the interface, you declare that all types derived from your type must implement that interface. The IMsg interface is part of the declaration of MyClass, but defining the methods is deferred to each derived class.<br/>Explicit interface implementation enables you to implement an interface, yet hide its members from the public interface of your type. Its use throws a few other twists into the relationships between implementing interfaces and overriding virtual functions. You use explicit interface implementation to limit client code from using the interface methods when a more appropriate version is available. The IComparable idiom in Item 26 shows this in detail.<br/>Implementing interfaces allows more options than creating and overriding virtual functions. You can cr&#101;ate sealed implementations, virtual implementations, o&#114; abstract contracts for class hierarchies. You can decide exactly how and when derived classes can modify the default behavior for members of any interface your class implements. Interface methods are not virtual methods, but a separate contract.</p><p> + <a href="#CommentCard">&#x67E5;&#x770B;&#x5F53;&#x524D;&#x65E5;&#x5FD7;&#x8BC4;&#x8BBA;</a> (0)</p><p>&nbsp;<br/><br/><a href="wap.asp?do=Login">&#x767B;&#x5F55;</a></p><p><br/>&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;</p><p><a href="wap.asp">&#x6728;&#x5B50;&#x5C4B;</a></p><p><a href="http://www.pjhome.net/wap.asp">PJBlog3&nbsp;v3.2.9.518</a>&nbsp;Inside.</p><p>Processed&nbsp;In&nbsp;0.078&nbsp;ms</p><do type="prev" label="&#x8FD4;&#x56DE;"><prev/></do></card><card id="postCommentCard"><p><b>&#x6807;&#x9898;&#x3A;</b> <a href="#MainCard">&#x45;&#x66;&#x66;&#x65;&#x63;&#x74;&#x69;&#x76;&#x65;&#x20;&#x43;&#x23;&#x20;&#x539F;&#x5219;&#x32;&#x30;&#xFF1A;&#x660E;&#x8FA8;&#x63A5;&#x53E3;&#x5B9E;&#x73B0;&#x548C;&#x865A;&#x51FD;&#x6570;&#x91CD;&#x8F7D;&#x7684;&#x533A;&#x522B;</a></p><p><br/>你没有权限发表评论</p><p><br/>&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;</p><p><a href="wap.asp">&#x6728;&#x5B50;&#x5C4B;</a></p><p><a href="http://www.pjhome.net/wap.asp">PJBlog3&nbsp;v3.2.9.518</a>&nbsp;Inside.</p><p>Processed&nbsp;In&nbsp;0.078&nbsp;ms</p><do type="prev" label="&#x8FD4;&#x56DE;"><prev/></do></card><card id="CommentCard"><p>&#x6682;&#x65E0;&#x8BC4;&#x8BBA;</p><p><a href="#MainCard">&#x8FD4;&#x56DE;</a></p><p><br/>&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;&#x2500;</p><p><a href="wap.asp">&#x6728;&#x5B50;&#x5C4B;</a></p><p><a href="http://www.pjhome.net/wap.asp">PJBlog3&nbsp;v3.2.9.518</a>&nbsp;Inside.</p><p>Processed&nbsp;In&nbsp;0.078&nbsp;ms</p><do type="prev" label="&#x8FD4;&#x56DE;"><prev/></do></card>
</wml>
