﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-housanren</title><link>http://www.blogjava.net/housanren/</link><description /><language>zh-cn</language><lastBuildDate>Thu, 07 May 2026 02:14:24 GMT</lastBuildDate><pubDate>Thu, 07 May 2026 02:14:24 GMT</pubDate><ttl>60</ttl><item><title>强制类型转换</title><link>http://www.blogjava.net/housanren/archive/2011/11/17/364065.html</link><dc:creator>后山人</dc:creator><author>后山人</author><pubDate>Thu, 17 Nov 2011 04:46:00 GMT</pubDate><guid>http://www.blogjava.net/housanren/archive/2011/11/17/364065.html</guid><wfw:comment>http://www.blogjava.net/housanren/comments/364065.html</wfw:comment><comments>http://www.blogjava.net/housanren/archive/2011/11/17/364065.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/housanren/comments/commentRss/364065.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/housanren/services/trackbacks/364065.html</trackback:ping><description><![CDATA[<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="font-family:宋体;">强制类型转换</span></p>  <p>&nbsp;</p>  <p><span style="font-family:宋体;">方法有两种</span></p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;">1．&nbsp;<span style="font-family:宋体;">向上类型转换（</span>upcast<span style="font-family:宋体;">）：比如说将</span>CAT<span style="font-family:宋体;">类型转换</span>Animal<span style="font-family:宋体;">类型</span> <span style="font-family: 宋体;">即将子类型转换为父类型</span></p>  <p style="margin-left:18.0pt;text-indent:0cm;"><span style="font-family:宋体;">不需要在前面加小括号</span> </p>  <p style="margin-left:18.0pt;text-indent:-18.0pt;">2．&nbsp;<span style="font-family:宋体;">向下类型转换</span>(downcast):<span style="font-family:宋体;">比如将</span>Animal<span style="font-family:宋体;">类型转换为</span>Cat<span style="font-family:宋体;">类型</span> <span style="font-family: 宋体;">即将父类转换为子类（必须要使用强制类型转换）</span></p>  <p style="margin-left:18.0pt">&nbsp;</p>  <p>&nbsp;</p>  <p>&nbsp;</p>  <p>&nbsp;<span style="font-family:宋体;">程序：</span></p>  <p align="left" style="text-align:left;text-autospace:none">&nbsp;</p>  <p align="left" style="text-align:left;text-autospace:none"><strong><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#7F0055;">public</span></strong> <strong><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#7F0055;">class</span></strong><span style="font-size: 9pt; font-family: 'Courier New'; "> PolyTest2</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">{</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:#7F0055;">public</span></strong> <strong><span style="font-size: 9.0pt;font-family:&quot;Courier New&quot;;color:#7F0055;">static</span></strong> <strong><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#7F0055;">void</span></strong><span style="font-size: 9pt; font-family: 'Courier New'; "> main(String[] args)</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;  &nbsp;</span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#3F7F5F;">/*Animal animal = new Cat();</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size:9.0pt;font-family: &quot;Courier New&quot;;color:#3F7F5F;">&nbsp;&nbsp;&nbsp;  &nbsp;Animal animal2 = new Animal();</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size:9.0pt;font-family: &quot;Courier New&quot;;color:#3F7F5F;">&nbsp;&nbsp;&nbsp;  &nbsp;</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size:9.0pt;font-family: &quot;Courier New&quot;;color:#3F7F5F;">&nbsp;&nbsp;&nbsp;  &nbsp;animal2 = animal;</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size:9.0pt;font-family: &quot;Courier New&quot;;color:#3F7F5F;">&nbsp;&nbsp;&nbsp;  &nbsp;animal2.sing();</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size:9.0pt;font-family: &quot;Courier New&quot;;color:#3F7F5F;">&nbsp;&nbsp;&nbsp;  */</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;  &nbsp;</span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#3F7F5F;">/* Animal animal = new Cat();</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size:9.0pt;font-family: &quot;Courier New&quot;;color:#3F7F5F;">&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;Animal animal2 = new Animal();</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size:9.0pt;font-family: &quot;Courier New&quot;;color:#3F7F5F;">&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size:9.0pt;font-family: &quot;Courier New&quot;;color:#3F7F5F;">&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;animal = animal2;</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size:9.0pt;font-family: &quot;Courier New&quot;;color:#3F7F5F;">&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;animal.sing();</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size:9.0pt;font-family: &quot;Courier New&quot;;color:#3F7F5F;">&nbsp;&nbsp;&nbsp;  &nbsp;*/</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;</span><span style="font-size: 9.0pt;font-family:&quot;Courier New&quot;;color:#3F7F5F;">//</span><span style="font-size:9.0pt;font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;;color:#3F7F5F;">向上类型转换</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;Cat cat = </span><strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:#7F0055;">new</span></strong><span style="font-size: 9pt; font-family: 'Courier New'; "> Cat();</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;Animal animal = cat;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="font-size: 9.0pt;font-family:&quot;Courier New&quot;;color:#3F7F5F;">//</span><span style="font-size:9.0pt;font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;;color:#3F7F5F;">当然使用这种方法写也是可以的</span><span style="font-size:9.0pt;font-family: &quot;Courier New&quot;;color:#3F7F5F;">&nbsp;&nbsp;&nbsp;&nbsp; Animal animal = (Animal)cat;</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;animal.sing();</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;</span><span style="font-size: 9.0pt;font-family:&quot;Courier New&quot;;color:#3F7F5F;">//</span><span style="font-size:9.0pt;font-family:宋体;Courier New&quot;;Courier New&quot;;Courier New&quot;;color:#3F7F5F;">向下类型转换</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Animal a = </span><strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:#7F0055;">new</span></strong><span style="font-size: 9pt; font-family: 'Courier New'; "> Cat();</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Cat <span style="background:yellow;">c</span> = (Cat)a;</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;  &nbsp;&nbsp;<span style="background:silver;">c</span>.sing();</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">}</span></p>  <p align="left" style="text-align:left;text-autospace:none">&nbsp;</p>  <p align="left" style="text-align:left;text-autospace:none"><strong><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#7F0055;">class</span></strong><span style="font-size: 9pt; font-family: 'Courier New'; "> Animal</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">{</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp; &nbsp; </span><strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:#7F0055;">public</span></strong> <strong><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:#7F0055;">void</span></strong><span style="font-size: 9pt; font-family: 'Courier New'; "> sing()</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp; &nbsp; {</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; System.</span><em><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:#0000C0;">out</span></em><span style="font-size: 9pt; font-family: 'Courier New'; ">.println(</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:#2A00FF;">"animal is sing"</span><span style="font-size: 9pt; font-family: 'Courier New'; ">);</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp; &nbsp; }</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">}</span></p>  <p align="left" style="text-align:left;text-autospace:none"><strong><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#7F0055;">class</span></strong><span style="font-size: 9pt; font-family: 'Courier New'; "> Dog </span><strong><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#7F0055;">extends</span></strong><span style="font-size: 9pt; font-family: 'Courier New'; "> Animal</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">{</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size:9.0pt;font-family: &quot;Courier New&quot;;color:#7F0055;">public</span></strong> <strong><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#7F0055;">void</span></strong><span style="font-size: 9pt; font-family: 'Courier New'; "> sing()</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp; {</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; System.</span><em><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:#0000C0;">out</span></em><span style="font-size: 9pt; font-family: 'Courier New'; ">.println(</span><span style="font-size:9.0pt;font-family:&quot;Courier New&quot;;color:#2A00FF;">"dog is sing"</span><span style="font-size: 9pt; font-family: 'Courier New'; ">);</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp; }</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">}</span></p>  <p align="left" style="text-align:left;text-autospace:none"><strong><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#7F0055;">class</span></strong><span style="font-size: 9pt; font-family: 'Courier New'; "> Cat </span><strong><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#7F0055;">extends</span></strong><span style="font-size: 9pt; font-family: 'Courier New'; "> Animal</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">{</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp; </span><strong><span style="font-size:9.0pt;font-family: &quot;Courier New&quot;;color:#7F0055;">public</span></strong> <strong><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#7F0055;">void</span></strong><span style="font-size: 9pt; font-family: 'Courier New'; "> sing()</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp; {</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.</span><em><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#0000C0;">out</span></em><span style="font-size: 9pt; font-family: 'Courier New'; ">.println(</span><span style="font-size:9.0pt; font-family:&quot;Courier New&quot;;color:#2A00FF;">"cat is sing "</span><span style="font-size: 9pt; font-family: 'Courier New'; ">);</span></p>  <p align="left" style="text-align:left;text-autospace:none"><span style="font-size: 9pt; font-family: 'Courier New'; ">&nbsp;&nbsp;&nbsp; }</span></p>  <p><span style="font-size: 9pt; font-family: 'Courier New'; ">}</span></p>  <p>&nbsp;</p>  <p>&nbsp;</p>  <p>&nbsp;</p><img src ="http://www.blogjava.net/housanren/aggbug/364065.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/housanren/" target="_blank">后山人</a> 2011-11-17 12:46 <a href="http://www.blogjava.net/housanren/archive/2011/11/17/364065.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>