<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>override</title>
	<atom:link href="https://www.aitaocui.cn/tag/230053/feed" rel="self" type="application/rss+xml" />
	<link>https://www.aitaocui.cn</link>
	<description>翡翠玉石爱好者聚集地</description>
	<lastBuildDate>Sat, 26 Nov 2022 22:29:51 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>

<image>
	<url>https://www.aitaocui.cn/wp-content/uploads/2022/11/taocui.png</url>
	<title>override</title>
	<link>https://www.aitaocui.cn</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>override(接口方法)</title>
		<link>https://www.aitaocui.cn/article/341814.html</link>
					<comments>https://www.aitaocui.cn/article/341814.html#respond</comments>
		
		<dc:creator><![CDATA[长老会]]></dc:creator>
		<pubDate>Sat, 26 Nov 2022 22:29:51 +0000</pubDate>
				<category><![CDATA[知识]]></category>
		<category><![CDATA[override]]></category>
		<guid isPermaLink="false">https://www.aitaocui.cn/?p=341814</guid>

					<description><![CDATA[override可以翻译为复盖，从字面就可以知道，它是复盖了一个方法并且对其重写，以求达到不同的作用。对我们来说最熟悉的复盖就是对接口方法的实现，在接口中一般只是对方法进行了声明，...]]></description>
										<content:encoded><![CDATA[</p>
<article>
<p>override可以翻译为复盖，从字面就可以知道，它是复盖了一个方法并且对其重写，以求达到不同的作用。对我们来说最熟悉的复盖就是对接口方法的实现，在接口中一般只是对方法进行了声明，而我们在实现时，就需要实现接口声明的所有方法。除了这个典型的用法以外，我们在继承中也可能会在子类复盖父类中的方法。</p>
</article>
<article>
<h1>注意事项</h1>
<h2 id="a-37b35a9d">覆盖</h2>
<p>1、覆盖的方法的标志必须要和被覆盖的方法的标志完全匹配，才能达到覆盖的效果；/n2、覆盖的方法的返回值必须和被覆盖的方法的返回一致；/n3、覆盖的方法所抛出的异常必须和被覆盖方法的所抛出的异常一致，或者是其子类；/n4、被覆盖的方法不能为private，否则在其子类中只是新定义了一个方法，并没有对其进行覆盖。</p>
<h2 id="a-d24068f2">重载</h2>
<p>overload对我们来说可能比较熟悉，可以翻译为重载，它是指我们可以定义一些名称相同的方法，通过定义不同的输入参数来区分这些方法，然后再调用时，VM就会根据不同的参数样式，来选择合适的方法执行。在使用重载要注意以下的几点：</p>
<p>1、在使用重载时只能通过不同的参数样式。例如，不同的参数类型，不同的参数个数，不同的参数顺序（当然，同一方法内的几个参数类型必须不一样，例如可以是fun(int, float)，但是不能为fun(int, int)）；</p>
<p>2、不能通过访问权限、返回类型、抛出的异常进行重载；</p>
<p>3、方法的异常类型和数目不会对重载造成影响；</p>
<p>4、对于继承来说，如果某一方法在父类中是访问权限是priavte，那么就不能在子类对其进行重载，如果定义的话，也只是定义了一个新方法，而不会达到重载的效果。</p>
<h1>测试程序</h1>
<p>下面是对override和overload的测试程序，其中注释中的内容都是会产生编译错误的代码，将注释去掉，看看在编译时会产生什么效果。/n// 对overload测试的文件：OverloadTest.java/npublic class OverloadTest {</p>
</p>
</article>
<div class="mt-3 mb-3" style="max-width: 770px;height: auto;">
                                    </div>
<div class="mt-3 mb-3" style="max-width: 770px;height: auto;">
                                    </div>
<div class="mt-3 mb-3" style="max-width: 770px;height: auto;">
                                    </div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.aitaocui.cn/article/341814.html/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
