
<?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;&#x38;&#xFF1A;&#x907F;&#x514D;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;</p><p><b>&#x4F5C;&#x8005;&#x3A;</b> &#x64;&#x6E;&#x61;&#x77;&#x6F;</p><p><b>&#x65E5;&#x671F;&#x3A;</b> &#x32;&#x30;&#x31;&#x30;&#x2D;&#x30;&#x31;&#x2D;&#x30;&#x32;&#x20;&#x31;&#x32;&#x3A;&#x33;&#x35;&#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> &#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x662F;&#x4E00;&#x79CD;&#x7B49;&#x4EE3;&#x7C7B;&#x578B;(Substitutability)&#x95F4;&#x64CD;&#x4F5C;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x3002;&#x7B49;&#x4EE3;&#x7C7B;&#x578B;&#x5C31;&#x662F;&#x6307;&#x4E00;&#x4E2A;&#x7C7B;&#x53EF;&#x4EE5;&#x53D6;&#x4EE3;&#x53E6;&#x4E00;&#x4E2A;&#x7C7B;&#x3002;&#x8FD9;&#x53EF;&#x80FD;&#x662F;&#x4EF6;&#x597D;&#x4E8B;&#xFF1A;&#x4E00;&#x4E2A;&#x6D3E;&#x751F;&#x7C7B;&#x7684;&#x5BF9;&#x8C61;&#x53EF;&#x4EE5;&#x88AB;&#x5B83;&#x57FA;&#x7C7B;&#x7684;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x53D6;&#x4EE3;&#xFF0C;&#x4E00;&#x4E2A;&#x7ECF;&#x5178;&#x7684;&#x4F8B;&#x5B50;&#x5C31;&#x662F;&#x5F62;&#x72B6;&#x7EE7;&#x627F;&#x3002;&#x5148;&#x6709;&#x4E00;&#x4E2A;&#x5F62;&#x72B6;&#x7C7B;&#xFF0C;&#x7136;&#x540E;&#x6D3E;&#x751F;&#x51FA;&#x5F88;&#x591A;&#x5176;&#x5B83;&#x7684;&#x7C7B;&#x578B;&#xFF1A;&#x957F;&#x65B9;&#x5F62;&#xFF0C;&#x692D;&#x5706;&#x5F62;&#xFF0C;&#x5706;&#x5F62;&#x4EE5;&#x53CA;&#x5176;&#x5B83;&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x4F55;&#x5730;&#x65B9;&#x7528;&#x56FE;&#x5F62;&#x72B6;&#x6765;&#x53D6;&#x4EE3;&#x5706;&#x5F62;&#xFF0C;&#x8FD9;&#x5C31;&#x662F;&#x591A;&#x6001;&#x7684;&#x7B49;&#x4EE3;&#x7C7B;&#x578B;&#x3002;&#x8FD9;&#x662F;&#x6B63;&#x786E;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x5706;&#x5F62;&#x5C31;&#x662F;&#x4E00;&#x4E2A;&#x7279;&#x6B8A;&#x7684;&#x5F62;&#x72B6;&#x3002;&#x5F53;&#x4F60;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x7C7B;&#x65F6;&#xFF0C;&#x660E;&#x786E;&#x7684;&#x7C7B;&#x578B;&#x8F6C;&#x5316;&#x662F;&#x53EF;&#x4EE5;&#x81EA;&#x52A8;&#x5B8C;&#x6210;&#x7684;&#x3002;&#x6B63;&#x5982;.Net&#x4E2D;&#x7C7B;&#x7684;&#x7EE7;&#x627F;&#xFF0C;&#x56E0;&#x4E3A;System.Object&#x662F;&#x6240;&#x6709;&#x7C7B;&#x578B;&#x7684;&#x57FA;&#x7C7B;&#xFF0C;&#x6240;&#x4EE5;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#x90FD;&#x53EF;&#x4EE5;&#x7528;System.Obejct&#x6765;&#x53D6;&#x4EE3;&#x3002;&#x540C;&#x6837;&#x7684;&#x60C5;&#x51B5;&#xFF0C;&#x4F60;&#x6240;&#x521B;&#x5EFA;&#x7684;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#xFF0C;&#x4E5F;&#x5E94;&#x8BE5;&#x53EF;&#x4EE5;&#x7528;&#x5B83;&#x6240;&#x5B9E;&#x73B0;&#x7684;&#x63A5;&#x53E3;&#x6765;&#x53D6;&#x4EE3;&#xFF0C;&#x6216;&#x8005;&#x7528;&#x5B83;&#x7684;&#x57FA;&#x7C7B;&#x63A5;&#x53E3;&#x6765;&#x53D6;&#x4EE3;&#xFF0C;&#x6216;&#x8005;&#x5C31;&#x7528;&#x57FA;&#x7C7B;&#x6765;&#x53D6;&#x4EE3;&#x3002;&#x4E0D;&#x4EC5;&#x5982;&#x6B64;&#xFF0C;C#&#x8BED;&#x8A00;&#x8FD8;&#x652F;&#x6301;&#x5F88;&#x591A;&#x5176;&#x5B83;&#x7684;&#x8F6C;&#x6362;&#x3002;<br/>&#x5F53;&#x4F60;&#x4E3A;&#x67D0;&#x4E2A;&#x7C7B;&#x578B;&#x6DFB;&#x52A0;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x65F6;&#xFF0C;&#x5C31;&#x7B49;&#x4E8E;&#x662F;&#x544A;&#x8BC9;&#x7F16;&#x8BD1;&#x5668;&#xFF1A;&#x4F60;&#x7684;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x88AB;&#x76EE;&#x6807;&#x7C7B;&#x6240;&#x53D6;&#x4EE3;&#x3002;&#x8FD9;&#x53EF;&#x80FD;&#x4F1A;&#x5F15;&#x53D1;&#x4E00;&#x4E9B;&#x6F5C;&#x5728;&#x7684;&#x9519;&#x8BEF;&#xFF0C;&#x56E0;&#x4E3A;&#x4F60;&#x7684;&#x7C7B;&#x578B;&#x5F88;&#x53EF;&#x80FD;&#x5E76;&#x4E0D;&#x80FD;&#x88AB;&#x76EE;&#x6807;&#x7C7B;&#x578B;&#x6240;&#x53D6;&#x4EE3;(&#x8BD1;&#x6CE8;&#xFF1A;&#x8FD9;&#x91CC;&#x5E76;&#x4E0D;&#x662F;&#x6307;&#x7EE7;&#x627F;&#x5173;&#x7CFB;&#x4E0A;&#x7684;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#xFF0C;&#x800C;&#x662F;C#&#x8BED;&#x8A00;&#x8BB8;&#x53EF;&#x6211;&#x4EEC;&#x7684;&#x53E6;&#x4E00;&#x79CD;&#x8F6C;&#x6362;&#xFF0C;&#x8BF7;&#x770B;&#x540E;&#x6587;)&#x3002;&#x5B83;&#x7684;&#x526F;&#x4F5C;&#x7528;&#x5C31;&#x662F;&#x4FEE;&#x6539;&#x4E86;&#x76EE;&#x6807;&#x7C7B;&#x578B;&#x7684;&#x72B6;&#x6001;&#x540E;&#x53EF;&#x80FD;&#x5BF9;&#x539F;&#x7C7B;&#x578B;&#x6839;&#x672C;&#x65E0;&#x6548;&#x3002;&#x66F4;&#x7CDF;&#x7CD5;&#x7684;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x7684;&#x8F6C;&#x6362;&#x4EA7;&#x751F;&#x4E86;&#x4E34;&#x65F6;&#x5BF9;&#x8C61;&#xFF0C;&#x90A3;&#x4E48;&#x526F;&#x4F5C;&#x7528;&#x5C31;&#x662F;&#x4F60;&#x76F4;&#x63A5;&#x4FEE;&#x6539;&#x4E86;&#x4E34;&#x65F6;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x4E14;&#x5B83;&#x4F1A;&#x6C38;&#x4E45;&#x4E22;&#x5931;&#x5728;&#x5783;&#x573E;&#x56DE;&#x6536;&#x5668;&#x3002;&#x603B;&#x4E4B;&#xFF0C;&#x4F7F;&#x7528;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x5E94;&#x8BE5;&#x57FA;&#x4E8E;&#x7F16;&#x8BD1;&#x65F6;&#x7684;&#x7C7B;&#x578B;&#x5BF9;&#x8C61;&#xFF0C;&#x800C;&#x4E0D;&#x662F;&#x8FD0;&#x884C;&#x65F6;&#x7684;&#x7C7B;&#x578B;&#x5BF9;&#x8C61;&#x3002;&#x7528;&#x6237;&#x53EF;&#x80FD;&#x987B;&#x8981;&#x5BF9;&#x7C7B;&#x578B;&#x8FDB;&#x884C;&#x591A;&#x6837;&#x5316;&#x7684;&#x5F3A;&#x5236;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#xFF0C;&#x8FD9;&#x6837;&#x7684;&#x5B9E;&#x9645;&#x64CD;&#x4F5C;&#x53EF;&#x80FD;&#x4EA7;&#x751F;&#x4E0D;&#x7EF4;&#x62A4;&#x7684;&#x4EE3;&#x7801;&#x3002;<br/>&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x628A;&#x4E00;&#x4E2A;&#x672A;&#x77E5;&#x7C7B;&#x578B;&#x8F6C;&#x5316;&#x4E3A;&#x4F60;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x8FD9;&#x4F1A;&#x66F4;&#x52A0;&#x6E05;&#x695A;&#x7684;&#x8868;&#x73B0;&#x521B;&#x5EFA;&#x65B0;&#x5BF9;&#x8C61;&#x7684;&#x64CD;&#x4F5C;(&#x8BD1;&#x6CE8;&#xFF1A;&#x8FD9;&#x6837;&#x7684;&#x8F6C;&#x6362;&#x662F;&#x8981;&#x521B;&#x5EFA;&#x65B0;&#x5BF9;&#x8C61;&#x7684;)&#x3002;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x4F1A;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x4EA7;&#x751F;&#x96BE;&#x4E8E;&#x53D1;&#x73B0;&#x7684;&#x95EE;&#x9898;&#x3002;&#x5047;&#x8BBE;&#x6709;&#x8FD9;&#x6837;&#x4E00;&#x79CD;&#x60C5;&#x51B5;&#xFF0C;&#x4F60;&#x521B;&#x5EFA;&#x4E86;&#x5982;&#x56FE;3.1&#x90A3;&#x6837;&#x7684;&#x7C7B;&#x5E93;&#x7ED3;&#x6784;&#x3002;&#x692D;&#x5706;&#x548C;&#x5706;&#x90FD;&#x662F;&#x4ECE;&#x5F62;&#x72B6;&#x7C7B;&#x7EE7;&#x627F;&#x4E0B;&#x6765;&#x7684;&#xFF0C;&#x5C3D;&#x7BA1;&#x4F60;&#x76F8;&#x4FE1;&#x692D;&#x5706;&#x548C;&#x5706;&#x662F;&#x76F8;&#x5173;&#x7684;&#xFF0C;&#x4F46;&#x8FD8;&#x662F;&#x51B3;&#x5B9A;&#x4FDD;&#x7559;&#x8FD9;&#x6837;&#x7684;&#x7EE7;&#x627F;&#x5173;&#x7CFB;&#x3002;&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x4F60;&#x4E0D;&#x60F3;&#x5728;&#x7EE7;&#x627F;&#x5173;&#x7CFB;&#x4E2D;&#x4F7F;&#x7528;&#x975E;&#x62BD;&#x8C61;&#x53F6;&#x5B50;&#x7C7B;&#xFF0C;&#x8FD9;&#x4F1A;&#x5728;&#x4ECE;&#x692D;&#x5706;&#x7C7B;&#x4E0A;&#x7EE7;&#x627F;&#x5706;&#x7C7B;&#x65F6;&#xFF0C;&#x6709;&#x4E00;&#x4E9B;&#x4E0D;&#x597D;&#x5B9E;&#x73B0;&#x7684;&#x96BE;&#x9898;&#x5B58;&#x5728;&#x3002;&#x7136;&#x800C;&#xFF0C;&#x4F60;&#x53C8;&#x610F;&#x8BC6;&#x5230;&#x6BCF;&#x4E00;&#x4E2A;&#x5706;&#x5F62;&#x5E94;&#x8BE5;&#x662F;&#x4E00;&#x4E2A;&#x692D;&#x5706;&#xFF0C;&#x53E6;&#x5916;&#x67D0;&#x4E9B;&#x692D;&#x5706;&#x4E5F;&#x53EF;&#x80FD;&#x662F;&#x5706;&#x5F62;&#x3002;<br/>(&#x56FE;3.1)<br/>(&#x8BD1;&#x6CE8;&#xFF1A;&#x8FD9;&#x4E00;&#x539F;&#x5219;&#x4E2D;&#x4F5C;&#x8005;&#x6240;&#x7ED9;&#x51FA;&#x7684;&#x4F8B;&#x5B50;&#x4E0D;&#x662F;&#x5F88;&#x6070;&#x5F53;&#xFF0C;&#x800C;&#x4E14;&#x4F5C;&#x8005;&#x4E5F;&#x5728;&#x524D;&#x9762;&#x5047;&#x8BBE;&#x4E86;&#x539F;&#x56E0;&#xFF0C;&#x56E0;&#x6B64;&#x8BF7;&#x8BFB;&#x8005;&#x4E0D;&#x8981;&#x5BF9;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x592A;&#x94BB;&#x725B;&#x89D2;&#x5C16;&#xFF0C;&#x7406;&#x89E3;&#x4F5C;&#x8005;&#x6240;&#x5728;&#x8868;&#x8FBE;&#x7684;&#x601D;&#x60F3;&#x5C31;&#x884C;&#x4E86;&#xFF0C;&#x76F8;&#x4FE1;&#x5728;&#x4F60;&#x7684;C#&#x5F00;&#x53D1;&#x4E2D;&#x53EF;&#x80FD;&#x4E5F;&#x4F1A;&#x9047;&#x5230;&#x7C7B;&#x4F3C;&#x7684;&#x8F6C;&#x6362;&#x95EE;&#x9898;&#xFF0C;&#x53EA;&#x662F;&#x4E0D;&#x592A;&#x53EF;&#x80FD;&#x4ECE;&#x5706;&#x5F62;&#x8F6C;&#x692D;&#x5706;&#x3002;)<br/>&#x8FD9;&#x5C06;&#x5BFC;&#x81F4;&#x4F60;&#x8981;&#x6DFB;&#x52A0;&#x4E24;&#x4E2A;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x3002;&#x56E0;&#x4E3A;&#x6BCF;&#x4E00;&#x4E2A;&#x5706;&#x5F62;&#x90FD;&#x662F;&#x4E00;&#x4E2A;&#x692D;&#x5706;&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x6DFB;&#x52A0;&#x9690;&#x5F0F;&#x8F6C;&#x6362;&#x4ECE;&#x4E00;&#x4E2A;&#x5706;&#x5F62;&#x8F6C;&#x6362;&#x5230;&#x65B0;&#x7684;&#x692D;&#x5706;&#x3002;&#x9690;&#x5F0F;&#x8F6C;&#x6362;&#x4F1A;&#x5728;&#x4E00;&#x4E2A;&#x7C7B;&#x8981;&#x6C42;&#x8F6C;&#x5316;&#x4E3A;&#x53E6;&#x4E00;&#x4E2A;&#x7C7B;&#x65F6;&#x88AB;&#x8C03;&#x7528;&#x3002;&#x5BF9;&#x5E94;&#x7684;&#xFF0C;&#x663E;&#x793A;&#x8F6C;&#x5316;&#x5C31;&#x662F;&#x7A0B;&#x5E8F;&#x5458;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x4F7F;&#x7528;&#x4E86;&#x5F3A;&#x5236;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x7B26;&#x3002;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class Circle : Shape<br/>{<br/>&nbsp;&nbsp;private PointF _center;<br/>&nbsp;&nbsp;private float _radius;<br/>&nbsp;&nbsp;public Circle() :<br/>&nbsp;&nbsp;&nbsp;&nbsp;this ( PointF.Empty, 0 )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public Circle( PointF c, float r )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;_center = c;<br/>&nbsp;&nbsp;&nbsp;&nbsp;_radius = r;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public override void Draw()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;//...<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;static public implicit operator Ellipse( Circle c )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return new Ellipse( c._center, c._center,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c._radius, c._radius );<br/>&nbsp;&nbsp;}<br/>}<br/>&#x73B0;&#x5728;&#x4F60;&#x5C31;&#x5DF2;&#x7ECF;&#x5B9E;&#x73B0;&#x4E86;&#x9690;&#x5F0F;&#x7684;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x5728;&#x4EFB;&#x4F55;&#x8981;&#x6C42;&#x692D;&#x5706;&#x7684;&#x5730;&#x65B9;&#x4F7F;&#x7528;&#x5706;&#x5F62;&#x3002;&#x800C;&#x4E14;&#x8FD9;&#x4E2A;&#x8F6C;&#x6362;&#x662F;&#x81EA;&#x52A8;&#x5B8C;&#x6210;&#x7684;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public double ComputeArea( Ellipse e )<br/>{<br/>&nbsp;&nbsp;// return the area of the ellipse.<br/>}<br/>// call it:<br/>Circle c = new Circle( new PointF( 3.0f, 0 ), 5.0f );<br/>ComputeArea( c );<br/>&#x6211;&#x53EA;&#x662F;&#x60F3;&#x7528;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x8868;&#x8FBE;&#x53EF;&#x66FF;&#x4EE3;&#x7C7B;&#x578B;&#xFF1A;&#x4E00;&#x4E2A;&#x5706;&#x5F62;&#x5DF2;&#x7ECF;&#x53EF;&#x4EE5;&#x4EE3;&#x66FF;&#x4E00;&#x4E2A;&#x53EF;&#x692D;&#x5706;&#x4E86;&#x3002;ComputeArea&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5728;&#x66FF;&#x4EE3;&#x7C7B;&#x578B;&#x4E0A;&#x5DE5;&#x4F5C;&#x3002;&#x4F60;&#x5F88;&#x5E78;&#x8FD0;&#xFF0C;&#x4F46;&#x770B;&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void Flatten( Ellipse e )<br/>{<br/>&nbsp;&nbsp;e.R1 /= 2;<br/>&nbsp;&nbsp;e.R2 *= 2;<br/>}<br/>// call it using a circle:<br/>Circle c = new Circle( new PointF ( 3.0f, 0 ), 5.0f );<br/>Flatten( c );<br/>&#x8FD9;&#x662F;&#x65E0;&#x6548;&#x7684;&#xFF0C;Flatten()&#x65B9;&#x6CD5;&#x8981;&#x6C42;&#x4E00;&#x4E2A;&#x692D;&#x5706;&#x505A;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x7F16;&#x8BD1;&#x5668;&#x5FC5;&#x987B;&#x4EE5;&#x67D0;&#x79CD;&#x65B9;&#x5F0F;&#x628A;&#x5706;&#x5F62;&#x8F6C;&#x5316;&#x4E3A;&#x692D;&#x5706;&#x3002;&#x786E;&#x5B9E;&#xFF0C;&#x4E5F;&#x5DF2;&#x7ECF;&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x9690;&#x5F0F;&#x7684;&#x8F6C;&#x6362;&#x3002;&#x800C;&#x4E14;&#x4F60;&#x8F6C;&#x6362;&#x4E5F;&#x88AB;&#x8C03;&#x7528;&#x4E86;&#xFF0C;Flatten()&#x65B9;&#x6CD5;&#x5F97;&#x5230;&#x7684;&#x53C2;&#x6570;&#x662F;&#x4ECE;&#x4F60;&#x7684;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x4E2D;&#x521B;&#x5EFA;&#x7684;&#x65B0;&#x7684;&#x692D;&#x5706;&#x5BF9;&#x8C61;&#x3002;&#x8FD9;&#x4E2A;&#x4E34;&#x65F6;&#x5BF9;&#x8C61;&#x88AB;Flatten()&#x51FD;&#x6570;&#x4FEE;&#x6539;&#xFF0C;&#x800C;&#x4E14;&#x5B83;&#x5F88;&#x5FEB;&#x6210;&#x4E3A;&#x5783;&#x573E;&#x5BF9;&#x8C61;&#x3002;&#x6B63;&#x662F;&#x56E0;&#x4E3A;&#x8FD9;&#x4E2A;&#x4E34;&#x65F6;&#x5BF9;&#x8C61;&#xFF0C;Flatten()&#x51FD;&#x6570;&#x4EA7;&#x751F;&#x4E86;&#x526F;&#x4F5C;&#x7528;&#x3002;&#x6700;&#x540E;&#x7684;&#x7ED3;&#x679C;&#x5C31;&#x662F;&#x8FD9;&#x4E2A;&#x5706;&#x5F62;&#x5BF9;&#x8C61;&#xFF0C;c&#xFF0C;&#x6839;&#x672C;&#x5C31;&#x6CA1;&#x6709;&#x53D1;&#x751F;&#x4EFB;&#x4F55;&#x6539;&#x53D8;&#x3002;&#x4ECE;&#x9690;&#x5F0F;&#x8F6C;&#x6362;&#x4FEE;&#x6539;&#x6210;&#x663E;&#x793A;&#x8F6C;&#x6362;&#x4E5F;&#x53EA;&#x662F;&#x5F3A;&#x8FEB;&#x7528;&#x6237;&#x8C03;&#x7528;&#x5F3A;&#x5236;&#x8F6C;&#x6362;&#x800C;&#x4EE5;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Circle c = new Circle( new PointF( 3.0f, 0 ), 5.0f );<br/>Flatten( ( Ellipse ) c );<br/>&#x539F;&#x5148;&#x7684;&#x95EE;&#x9898;&#x8FD8;&#x662F;&#x5B58;&#x5728;&#x3002;&#x4F60;&#x60F3;&#x8BA9;&#x7528;&#x6237;&#x8C03;&#x7528;&#x5F3A;&#x5236;&#x8F6C;&#x6362;&#x4E3A;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF0C;&#x4F46;&#x5B9E;&#x9645;&#x4E0A;&#x8FD8;&#x662F;&#x4EA7;&#x751F;&#x4E86;&#x4E34;&#x65F6;&#x5BF9;&#x8C61;&#xFF0C;&#x628A;&#x4E34;&#x65F6;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x53D8;&#x5E73;(flatten)&#x64CD;&#x4F5C;&#x540E;&#x5C31;&#x4E22;&#x6389;&#x4E86;&#x3002;&#x539F;&#x6765;&#x7684;&#x5706;&#xFF0C;c&#xFF0C;&#x8FD8;&#x662F;&#x6839;&#x672C;&#x6CA1;&#x6709;&#x88AB;&#x4FEE;&#x6539;&#x8FC7;&#x3002;&#x53D6;&#x800C;&#x4EE3;&#x4E4B;&#x7684;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x4F60;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6784;&#x9020;&#x51FD;&#x6570;&#x628A;&#x5706;&#x5F62;&#x8F6C;&#x6362;&#x6210;&#x692D;&#x5706;&#xFF0C;&#x90A3;&#x4E48;&#x64CD;&#x4F5C;&#x5C31;&#x5F88;&#x660E;&#x786E;&#x4E86;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Circle c = new Circle( new PointF( 3.0f, 0 ), 5.0f );<br/>Flatten ( new Ellipse( c ));<br/>&#x76F8;&#x4FE1;&#x5F88;&#x591A;&#x7A0B;&#x5E8F;&#x5458;&#x4E00;&#x773C;&#x5C31;&#x770B;&#x7684;&#x51FA;&#x6765;&#xFF0C;&#x5728;&#x524D;&#x9762;&#x7684;&#x4E24;&#x884C;&#x4EE3;&#x7801;&#x4E2D;&#x4F20;&#x7ED9;Flatten()&#x7684;&#x692D;&#x5706;&#x5728;&#x4FEE;&#x6539;&#x540E;&#x5C31;&#x4E22;&#x5931;&#x4E86;&#x3002;&#x4ED6;&#x4EEC;&#x53EF;&#x80FD;&#x4F1A;&#x901A;&#x8FC7;&#x8DDF;&#x8E2A;&#x5BF9;&#x8C61;&#x6765;&#x89E3;&#x51B3;&#x8FD9;&#x4E2A;&#x95EE;&#x9898;&#xFF1A;<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Circle c = new Circle( new PointF( 3.0f, 0 ), 5.0f );<br/>// Work with the circle.<br/>// ...<br/>// Convert to an ellipse.<br/>Ellipse e = new Ellipse( c );<br/>Flatten( e );<br/>&#x901A;&#x8FC7;&#x4E00;&#x4E2A;&#x53D8;&#x91CF;&#x6765;&#x4FDD;&#x5B58;&#x4FEE;&#x6539;(&#x53D8;&#x5E73;)&#x540E;&#x7684;&#x692D;&#x5706;&#xFF0C;&#x901A;&#x8FC7;&#x6784;&#x9020;&#x51FD;&#x6570;&#x6765;&#x66FF;&#x6362;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#xFF0C;&#x4F60;&#x4E0D;&#x4F1A;&#x4E22;&#x5931;&#x4EFB;&#x4F55;&#x529F;&#x80FD;&#xFF1A;&#x4F60;&#x53EA;&#x662F;&#x8BA9;&#x521B;&#x5EFA;&#x65B0;&#x5BF9;&#x8C61;&#x7684;&#x64CD;&#x4F5C;&#x66F4;&#x52A0;&#x6E05;&#x695A;&#x3002;(&#x6709;&#x7ECF;&#x9A8C;&#x7684;C++&#x7A0B;&#x5E8F;&#x53EF;&#x80FD;&#x6CE8;&#x610F;&#x5230;C#&#x7684;&#x9690;&#x5F0F;&#x8F6C;&#x5316;&#x548C;&#x663E;&#x793A;&#x8F6C;&#x6362;&#x90FD;&#x6CA1;&#x6709;&#x8C03;&#x7528;&#x6784;&#x9020;&#x51FD;&#x6570;&#x3002;&#x5728;C++&#x4E2D;&#xFF0C;&#x53EA;&#x6709;&#x660E;&#x786E;&#x7684;&#x4F7F;&#x7528;new&#x64CD;&#x4F5C;&#x7B26;&#x624D;&#x80FD;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x5BF9;&#x8C61;&#x65F6;&#xFF0C;&#x5176;&#x5B83;&#x65F6;&#x5019;&#x4E0D;&#x884C;&#x3002;&#x800C;&#x5728;C#&#x7684;&#x6784;&#x9020;&#x51FD;&#x6570;&#x4E2D;&#x4E0D;&#x7528;&#x660E;&#x786E;&#x7684;&#x4F7F;&#x7528;&#x5173;&#x952E;&#x5B57;&#x3002;)<br/>&#x4ECE;&#x7C7B;&#x578B;&#x91CC;&#x8FD4;&#x56DE;&#x5B57;&#x6BB5;&#x7684;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x5E76;&#x4E0D;&#x4F1A;&#x5C55;&#x793A;&#x7C7B;&#x578B;&#x7684;&#x884C;&#x4E3A;&#xFF0C;&#x8FD9;&#x4F1A;&#x4EA7;&#x751F;&#x4E00;&#x4E9B;&#x95EE;&#x9898;&#x3002;&#x4F60;&#x7ED9;&#x7C7B;&#x578B;&#x7684;&#x5C01;&#x88C5;&#x539F;&#x5219;&#x7559;&#x4E0B;&#x4E86;&#x51E0;&#x4E2A;&#x4E25;&#x91CD;&#x7684;&#x6F0F;&#x6D1E;&#x3002;&#x901A;&#x8FC7;&#x628A;&#x7C7B;&#x578B;&#x5F3A;&#x5236;&#x8F6C;&#x5316;&#x4E3A;&#x5176;&#x5B83;&#x7C7B;&#x578B;&#xFF0C;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5230;&#x7C7B;&#x578B;&#x7684;&#x5185;&#x90E8;&#x53D8;&#x91CF;&#x3002;&#x8FD9;&#x6B63;&#x662F;&#x539F;&#x5219;23&#x4E2D;&#x6240;&#x8BA8;&#x8BBA;&#x7684;&#x6240;&#x6709;&#x539F;&#x56E0;&#x4E2D;&#x6700;&#x5E94;&#x8BE5;&#x907F;&#x514D;&#x7684;&#x3002;<br/>&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x79CD;&#x7C7B;&#x578B;&#x53EF;&#x66FF;&#x4EE3;&#x7684;&#x5F62;&#x5F0F;&#xFF0C;&#x4F46;&#x8FD9;&#x4F1A;&#x7ED9;&#x4EE3;&#x7801;&#x5F15;&#x53D1;&#x4E00;&#x4E9B;&#x95EE;&#x9898;&#x3002;&#x4F60;&#x5E94;&#x8BE5;&#x5DF2;&#x7ECF;&#x660E;&#x767D;&#x6240;&#x6709;&#x8FD9;&#x4E9B;&#x5185;&#x5BB9;&#xFF1A;&#x7528;&#x6237;&#x5E0C;&#x671B;&#x53EF;&#x4EE5;&#x5408;&#x7406;&#x7684;&#x7528;&#x67D0;&#x79CD;&#x7C7B;&#x578B;&#x6765;&#x66FF;&#x4EE3;&#x4F60;&#x7684;&#x7C7B;&#x578B;&#x3002;&#x5F53;&#x8FD9;&#x4E2A;&#x53EF;&#x66FF;&#x4EE3;&#x7C7B;&#x578B;&#x88AB;&#x8BBF;&#x95EE;&#x65F6;&#xFF0C;&#x4F60;&#x5C31;&#x8BA9;&#x7528;&#x6237;&#x5728;&#x4E34;&#x65F6;&#x5BF9;&#x8C61;&#x4E0A;&#x5DE5;&#x4F5C;&#xFF0C;&#x6216;&#x8005;&#x5185;&#x90E8;&#x5B57;&#x6BB5;&#x53D6;&#x4EE3;&#x4E86;&#x4F60;&#x521B;&#x5EFA;&#x7684;&#x7C7B;&#x3002;&#x968F;&#x540E;&#x4F60;&#x53EF;&#x80FD;&#x4FEE;&#x6539;&#x4E86;&#x4E34;&#x65F6;&#x5BF9;&#x8C61;&#xFF0C;&#x7136;&#x540E;&#x4E22;&#x6389;&#x3002;&#x56E0;&#x4E3A;&#x8FD9;&#x4E9B;&#x8F6C;&#x6362;&#x4EE3;&#x7801;&#x662F;&#x7F16;&#x8BD1;&#x5668;&#x4EA7;&#x751F;&#x7684;&#xFF0C;&#x56E0;&#x6B64;&#x8FD9;&#x4E9B;&#x6F5C;&#x5728;&#x7684;BUG&#x5F88;&#x96BE;&#x53D1;&#x73B0;&#x3002;&#x5E94;&#x8BE5;&#x5C3D;&#x91CF;&#x907F;&#x514D;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;&#x3002;<br/>========================================================&nbsp;&nbsp;&nbsp;&nbsp;<br/>Item 28: Avoid Conversion Operators<br/>Conversion operators introduce a kind of substitutability between classes. Substitutability means that one class can be substituted for another. This can be a benefit: An object of a derived class can be substituted for an object of its base class, as in the classic example of the shape hierarchy. You cr&#101;ate a Shape base class and derive a variety of customizations: Rectangle, Ellipse, Circle, and so on. You can substitute a Circle anywh&#101;re a Shape is expected. That&#39;s using polymorphism for substitutability. It works because a circle is a specific type of shape. When you cr&#101;ate a class, certain conversions are allowed automatically. Any object can be substituted for an instance of System.Object, the root of the .NET class hierarchy. In the same fashion, any object of a class that you cr&#101;ate will be substituted implicitly for an interface that it implements, any of its base interfaces, o&#114; any of its base classes. The language also supports a variety of numeric conversions.<br/>When you define a conversion operator for your type, you tell the compiler that your type may be substituted for the target type. These substitutions often result in subtle errors because your type probably isn&#39;t a perfect substitute for the target type. Side effects that modify the state of the target type won&#39;t have the same effect on your type. Worse, if your conversion operator returns a temporary object, the side effects will modify the temporary object and be lost forever to the garbage collector. Finally, the rules for invoking conversion operators are based on the compile-time type of an object, not the runtime type of an object. Users of your type might need to perform multiple casts to invoke the conversion operators, a practice that leads to unmaintainable code.<br/>If you want to convert another type into your type, use a constructor. This more clearly reflects the action of creating a new object. Conversion operators can introduce hard-to-find problems in your code. Suppose that you inherit the code for a library shown in Figure 3.1. Both the Circle class and the Ellipse class are derived from the Shape class. You decide to leave that hierarchy in place because you believe that, although the Circle and Ellipse are related, you don&#39;t want to have nonabstract leaf classes in your hierarchy, and several implementation problems occur when you try to derive the Circle class from the Ellipse class. However, you realize that every circle could be an ellipse. In addition, some ellipses could be substituted for circles.<br/>Figure 3.1. Basic shape hierarchy. <br/>That leads you to add two conversion operators. Every Circle is an Ellipse, so you add an implicit conversion to cr&#101;ate a new Ellipse from a Circle. An implicit conversion operator will be called whenever one type needs to be converted to another type. By contrast, an explicit conversion will be called only when the programmer puts a cast operator in the source code.<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public class Circle : Shape<br/>{<br/>&nbsp;&nbsp;private PointF _center;<br/>&nbsp;&nbsp;private float _radius;<br/>&nbsp;&nbsp;public Circle() :<br/>&nbsp;&nbsp;&nbsp;&nbsp;this ( PointF.Empty, 0 )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public Circle( PointF c, float r )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;_center = c;<br/>&nbsp;&nbsp;&nbsp;&nbsp;_radius = r;<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;public override void Draw()<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;//...<br/>&nbsp;&nbsp;}<br/>&nbsp;&nbsp;static public implicit operator Ellipse( Circle c )<br/>&nbsp;&nbsp;{<br/>&nbsp;&nbsp;&nbsp;&nbsp;return new Ellipse( c._center, c._center,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c._radius, c._radius );<br/>&nbsp;&nbsp;}<br/>}<br/>Now that you&#39;ve got the implicit conversion operator, you can use a Circle anywh&#101;re an Ellipse is expected. Furthermore, the conversion happens automatically:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public double ComputeArea( Ellipse e )<br/>{<br/>&nbsp;&nbsp;// return the area of the ellipse.<br/>}<br/>// call it:<br/>Circle c = new Circle( new PointF( 3.0f, 0 ), 5.0f );<br/>ComputeArea( c );<br/>This sample shows what I mean by substitutability: A circle has been substituted for an ellipse. The ComputeArea function works even with the substitution. You got lucky. But examine this function:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;public void Flatten( Ellipse e )<br/>{<br/>&nbsp;&nbsp;e.R1 /= 2;<br/>&nbsp;&nbsp;e.R2 *= 2;<br/>}<br/>// call it using a circle:<br/>Circle c = new Circle( new PointF ( 3.0f, 0 ), 5.0f );<br/>Flatten( c );<br/>This won&#39;t work. The Flatten() method takes an ellipse as an argument. The compiler must somehow convert a circle to an ellipse. You&#39;ve cr&#101;ated an implicit conversion that does exactly that. Your conversion gets called, and the Flatten() function receives as its parameter the ellipse cr&#101;ated by your implicit conversion. This temporary object is modified by the Flatten() function and immediately becomes garbage. The side effects expected from your Flatten() function occur, but only on a temporary object. The end result is that nothing happens to the circle, c.<br/>Changing the conversion from implicit to explicit only forces users to add a cast to the call:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Circle c = new Circle( new PointF( 3.0f, 0 ), 5.0f );<br/>Flatten( ( Ellipse ) c );<br/>The o&#114;iginal problem remains. You just forced your users to add a cast to cause the problem. You still cr&#101;ate a temporary object, flatten the temporary object, and throw it away. The circle, c, is not modified at all. Instead, if you cr&#101;ate a constructor to convert the Circle to an Ellipse, the actions are clearer:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Circle c = new Circle( new PointF( 3.0f, 0 ), 5.0f );<br/>Flatten ( new Ellipse( c ));<br/>Most programmers would see the previous two lines and immediately realize that any modifications to the ellipse passed to Flatten() are lost. They would fix the problem by keeping track of the new object:<br/>&#x590D;&#x5236;&#x5185;&#x5BB9;&#x5230;&#x526A;&#x8D34;&#x677F; &#x7A0B;&#x5E8F;&#x4EE3;&#x7801;Circle c = new Circle( new PointF( 3.0f, 0 ), 5.0f );<br/>// Work with the circle.<br/>// ...<br/>// Convert to an ellipse.<br/>Ellipse e = new Ellipse( c );<br/>Flatten( e );<br/>The variable e holds the flattened ellipse. By replacing the conversion operator with a constructor, you have not lost any functionality; you&#39;ve merely made it clearer when new objects are cr&#101;ated. (Veteran C++ programmers should note that C# does not call constructors for implicit o&#114; explicit conversions. You cr&#101;ate new objects only when you explicitly use the new operator, and at no other time. There is no need for the explicit keyword on constructors in C#.)<br/>Conversion operators that return fields inside your objects will not exhibit this behavior. They have other problems. You&#39;ve poked a serious hole in the encapsulation of your class. By casting your type to some other object, clients of your class can access an internal variable. That&#39;s best avoided for all the reasons discussed in Item 23.<br/>Conversion operators introduce a form of substitutability that causes problems in your code. You&#39;re indicating that, in all cases, users can reasonably expect that another class can be used in place of the one you cr&#101;ated. When this substituted object is accessed, you cause clients to work with temporary objects o&#114; internal fields in place of the class you cr&#101;ated. You then modify temporary objects and discard the results. These subtle bugs are hard to find because the compiler generates code to convert these objects. Avoid conversion operators.</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.074&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;&#x38;&#xFF1A;&#x907F;&#x514D;&#x8F6C;&#x6362;&#x64CD;&#x4F5C;</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.074&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.074&nbsp;ms</p><do type="prev" label="&#x8FD4;&#x56DE;"><prev/></do></card>
</wml>
