<?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>Kotlin &#8211; 张三太爷</title>
	<atom:link href="https://www.somedoc.net/tag/kotlin/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.somedoc.net</link>
	<description>看前面，黑洞洞</description>
	<lastBuildDate>Sat, 04 Jun 2022 04:23:17 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.1</generator>

<image>
	<url>https://www.somedoc.net/wp-content/uploads/2016/12/cropped-dandycheung-1-32x32.jpg</url>
	<title>Kotlin &#8211; 张三太爷</title>
	<link>https://www.somedoc.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Java 和 Kotlin 中 interface 的不同导致混合编程时出现的一个问题</title>
		<link>https://www.somedoc.net/2022/06/04/java-%e5%92%8c-kotlin-%e4%b8%ad-interface-%e7%9a%84%e4%b8%8d%e5%90%8c%e5%af%bc%e8%87%b4%e6%b7%b7%e5%90%88%e7%bc%96%e7%a8%8b%e6%97%b6%e5%87%ba%e7%8e%b0%e7%9a%84%e4%b8%80%e4%b8%aa%e9%97%ae%e9%a2%98/</link>
					<comments>https://www.somedoc.net/2022/06/04/java-%e5%92%8c-kotlin-%e4%b8%ad-interface-%e7%9a%84%e4%b8%8d%e5%90%8c%e5%af%bc%e8%87%b4%e6%b7%b7%e5%90%88%e7%bc%96%e7%a8%8b%e6%97%b6%e5%87%ba%e7%8e%b0%e7%9a%84%e4%b8%80%e4%b8%aa%e9%97%ae%e9%a2%98/#respond</comments>
		
		<dc:creator><![CDATA[张三太爷]]></dc:creator>
		<pubDate>Sat, 04 Jun 2022 04:23:17 +0000</pubDate>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Kotlin]]></category>
		<guid isPermaLink="false">https://www.somedoc.net/?p=5030</guid>

					<description><![CDATA[让小兄弟做一些新的技术研究，他兴奋地用上了垂涎已久的 Kot <a href="https://www.somedoc.net/2022/06/04/java-%e5%92%8c-kotlin-%e4%b8%ad-interface-%e7%9a%84%e4%b8%8d%e5%90%8c%e5%af%bc%e8%87%b4%e6%b7%b7%e5%90%88%e7%bc%96%e7%a8%8b%e6%97%b6%e5%87%ba%e7%8e%b0%e7%9a%84%e4%b8%80%e4%b8%aa%e9%97%ae%e9%a2%98/" class="more-link">[&#8230;]</a>]]></description>
										<content:encoded><![CDATA[<p>让小兄弟做一些新的技术研究，他兴奋地用上了垂涎已久的 Kotlin 语言，并大加赞赏。其中有个场景是，让他设计一套图像特效的接口与实现。</p>
<p>他用 Kotlin 欢快地写成了这样：</p><pre class="crayon-plain-tag">interface Effect {
    fun apply(input: Bitmap) : Bitmap
}</pre><p>并以之为基础接口，至少实现了三个特效。</p>
<p>我在 review 的时候哭笑不得，因为仍然把我提醒过的选项参数给丢掉了，今天我想修复一下。最简单的办法我是知道的，直接向上述 <code>apply</code> 方法中增加一个参数，然后再把那三个实现相应调整过来，再把相关调用也调整过来即可。</p>
<p>但这不符合我作为一个“勤奋的懒人”的直觉。我的第一目标通常是，尽可能少地修改供给侧，尽量不修改消费侧。是否可以新增一个接口方法，原型上加入选项参数，然后将当前这一接口方法改为对新方法的将选型参数置为空的调用呢？经过摸索，以下代码出炉：</p><pre class="crayon-plain-tag">interface Effect {
    class Options {
        companion object {
            val default: Options = Options()
        }
    }

    fun apply(input: Bitmap) : Bitmap {
        return apply(input, Options.default)
    }

    fun apply(input: Bitmap, options: Options) : Bitmap
}</pre><p>然后再去各个具体实现类中把函数签名中加入 <code>options: Effect.Options</code> 这一新的参数即可。点击编译，顺利通过。作为一个 Kotlin 菜鸟，真是要乐出花来。</p>
<p>然而收获胡萝卜结束不久，大棒随之而来。由于我是“一个 Kotlin 菜鸟”，所以在我想新写一个具体特效时，我决定还是使用 Java（主要原因是现成代码会多一些）。我开开心心地写下如下代码后，惊讶地发现，IDE 提示该类对于派生而来的接口的实现不够完整：</p><pre class="crayon-plain-tag">public class ShadowEffect implements Effect {
    public Bitmap apply(Bitmap input, Effect.Options options) {
        return null;
    }
}</pre><p>Java 需要同时实现两个接口函数，成为这样：</p><pre class="crayon-plain-tag">public class ShadowEffect implements Effect {
    public Bitmap apply(Bitmap input) {
        return apply(input, null);
    }

    public Bitmap apply(Bitmap input, Effect.Options options) {
        return null;
    }
}</pre><p>很显然，在 Kotlin 中，<code>interface</code> 同时接纳了 Java 的 <code>abstract</code> 类的部分特性，允许带有具有实现代码的接口函数存在；而 Java 对派生而来的 Kotlin 接口，只接纳其接口函数声明部分，对其具有的实现予以了摒弃。也就是说，将来的其它实现了 <code>Effect</code> 接口的 Java 类，也都要提供自己版本的 <code>Bitmap apply(Bitmap input)</code> 实现，不能从 Kotlin 的现成 <code>fun apply(input: Bitmap) : Bitmap</code> 实现中受益。</p>
<p>如果为了这样一个共同实现的函数的复用，一个可选的（但是笨拙的）方法是，提供一个新的 Java 基类，如下：</p><pre class="crayon-plain-tag">public abstract class JavaEffect implements Effect {
    public Bitmap apply(Bitmap input) {
        return apply(input, null);
    }
}</pre><p>然后将具体实现类的基类调整为它，并把 <code>implements</code> 转换为 <code>extends</code>。当然，在某种程度上这是令人沮丧的，毕竟抽象类跟接口不是一回事。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.somedoc.net/2022/06/04/java-%e5%92%8c-kotlin-%e4%b8%ad-interface-%e7%9a%84%e4%b8%8d%e5%90%8c%e5%af%bc%e8%87%b4%e6%b7%b7%e5%90%88%e7%bc%96%e7%a8%8b%e6%97%b6%e5%87%ba%e7%8e%b0%e7%9a%84%e4%b8%80%e4%b8%aa%e9%97%ae%e9%a2%98/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
