Kotlin ã®ã¯ã©ã¹å§è²ã¨ããããã£å§è²ãµãã¼ãæ©è½
Kotlin Vocabulary: å§è²
ä»äºãå®äºããæ¹æ³ã® 1 ã¤ã¯、ãã®ä»äºãä»è ã«å§è²ãããã¨ã§ã。çããã®ä»äºãåã ã¡ã«å§è²ãããã¨ãè¨ã£ã¦ããããã§ã¯ããã¾ãã。ä»åã®ãã¼ãã¯、ãããªãã¸ã§ã¯ãããå¥ã®ãªãã¸ã§ã¯ãã«å§è²ãããã¨ã§ã。
ã½ããã¦ã§ã¢ã®ä¸çã§ã¯、å§è²ã¨ããèãæ¹ã¯æ°ãããã®ã§ã¯ããã¾ãã。å§è²ã¯ãã¶ã¤ã³ ãã¿ã¼ã³ã® 1 ã¤ã§、ãããªãã¸ã§ã¯ãã ããªã²ã¼ãã¨å¼ã°ãããã«ãã¼ ãªãã¸ã§ã¯ãã«å§è²ãããã¨ã§ãªã¯ã¨ã¹ããå¦çãããã¨ãæãã¾ã。ããªã²ã¼ãã®å½¹å²ã¯、å ã®ãªãã¸ã§ã¯ãã«ä»£ãã£ã¦ãªã¯ã¨ã¹ããå¦çã、ãã®çµæãå ã®ãªãã¸ã§ã¯ããå©ç¨ã§ããããã«ãããã¨ã§ã。
Kotlin ã¯ã¯ã©ã¹å§è²ã¨ããããã£å§è²ããµãã¼ããã¦ããã®ã§、å§è²ãç°¡åã«æ±ãã¾ã。ããã«、ããã¤ãã®ç¬èªã®çµã¿è¾¼ã¿ããªã²ã¼ããæä¾ãã¦ãã¾ã。
ã¯ã©ã¹å§è²
æå¾ã«åé¤ãããé
ç®ã復å
ã§ãã ArrayList
ã使ãã¨ãã¾ããã。åºæ¬çã«、å¿
è¦ãªã®ã¯åã ArrayList
ã®æ©è½ã ãã§ãã、æå¾ã«åé¤ãããé
ç®ã¸ã®åç
§ãå¿
è¦ã§ã。
ãããå®ç¾ããæ¹æ³ã® 1 ã¤ã¯、ArrayList
ã¯ã©ã¹ãæ¡å¼µãããã¨ã§ã。ãã®æ°ããã¯ã©ã¹ã¯、MutableList
ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£
ã§ã¯ãªã ArrayList
ã®å
·è±¡ã¯ã©ã¹ãæ¡å¼µãããã®ãªã®ã§、ArrayList
ã®å
·è±¡ã¯ã©ã¹ã®å®è£
ã¨å¼·ãçµåããããã¨ã«ãªãã¾ã。
MutableList
ã®å®è£
㧠remove()
é¢æ°ããªã¼ãã¼ã©ã¤ãã、åé¤ããé
ç®ã®åç
§ãä¿æã§ããããã«ããããã§、ãã®ä»ã®ç©ºã®å®è£
ãä»ã®ãªãã¸ã§ã¯ãã«å§è²ãããã¨æã£ããã¨ã¯ããã¾ããã?Kotlin ã§ã¯、ãããå®ç¾ããæ¹æ³ãæä¾ããã¦ãã¾ã。å
·ä½çã«ã¯、å
é¨ ArrayList
ã¤ã³ã¹ã¿ã³ã¹ã«ä½æ¥ã®å¤§åãå§è²ã、ãã®åä½ãã«ã¹ã¿ãã¤ãºã§ãã¾ã。ãããè¡ããã、Kotlin ã«ã¯æ°ãããã¼ã¯ã¼ã by
ãå°å
¥ããã¦ãã¾ã。
ã§ã¯、ã¯ã©ã¹å§è²ã®ä»çµã¿ã確èªãã¦ã¿ã¾ããã。by
ãã¼ã¯ã¼ãã使ãã¨、Kotlin 㯠innerList
ã¤ã³ã¹ã¿ã³ã¹ãããªã²ã¼ãã¨ãã¦ä½¿ç¨ããã³ã¼ããèªåçã«çæãã¾ã。
<!-- Copyright 2019 Google LLC.SPDX-License-Identifier: Apache-2.0 -->
class ListWithTrash <T>(
private val innerList: MutableList<T> = ArrayList<T>()
) : MutableCollection<T> by innerList {
var deletedItem : T? = null
override fun remove(element: T): Boolean {
deletedItem = element
return innerList.remove(element)
}
fun recover(): T? {
return deletedItem
}
}
by
ãã¼ã¯ã¼ãã¯、MutableList
ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®æ©è½ã innerList
ã¨ããååã®å
é¨ ArrayList
ã¤ã³ã¹ã¿ã³ã¹ã«å§è²ãããã Kotlin ã«ä¼ãã¾ã。å
é¨ ArrayList
ãªãã¸ã§ã¯ãã«ç´æ¥æ©æ¸¡ãããã¡ã½ãããæä¾ãããã®ã§、ListWithTrash
㯠MutableList
ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®ãã¹ã¦ã®æ©è½ããµãã¼ããã¾ã。ããã«、ç¬èªã®åä½ã追å ãããã¨ãã§ããããã«ãªãã¾ã。
å é¨å¦ç
åä½ã®ä»çµã¿ã確èªãã¦ã¿ã¾ããã。ListWithTrash
ã®ãã¤ãã³ã¼ããéã³ã³ãã¤ã«ãã Java ã³ã¼ããè¦ãã¨、Kotlin ã³ã³ãã¤ã©ãå®éã«ã©ããã¼é¢æ°ãä½æãã¦ãããã¨ã確èªã§ãã¾ã。ãã®ã©ããã¼é¢æ°ã、å
é¨ ArrayList
ãªãã¸ã§ã¯ãã®å¯¾å¿ããé¢æ°ãå¼ã³åºãã¦ãããã¨ããããã¾ã。
public final class ListWithTrash implements Collection, KMutableCollection {@Nullable
private Object deletedItem;
private final List innerList;
@Nullable
public final Object getDeletedItem() {
return this.deletedItem;
}
public final void setDeletedItem(@Nullable Object var1) {
this.deletedItem = var1;
}
public boolean remove(Object element) {
this.deletedItem = element;
return this.innerList.remove(element);
}
@Nullable
public final Object recover() {
return this.deletedItem;
}
public ListWithTrash() {
this((List)null, 1, (DefaultConstructorMarker)null);
}
public int getSize() {
return this.innerList.size();
}
// $FF: bridge method
public final int size() {
return this.getSize();
}
//...and so on
}
注: çæãããã³ã¼ãã§、Kotlin ã³ã³ãã¤ã©ã¯ Decorator ãã¿ã¼ã³ã¨ããå¥ã®ãã¶ã¤ã³ ãã¿ã¼ã³ã使ã£ã¦ã¯ã©ã¹å§è²ããµãã¼ããã¦ãã¾ã。Decorator ãã¿ã¼ã³ã§ã¯、ãã³ã¬ã¼ã¿ ã¯ã©ã¹ããã³ã¬ã¼ããããã¯ã©ã¹ã¨åãã¤ã³ã¿ã¼ãã§ã¼ã¹ãå ±æãã¾ã。ãã³ã¬ã¼ã¿ ã¯ã©ã¹ã¯、ã¿ã¼ã²ãã ã¯ã©ã¹ã®å é¨åç §ãä¿æã、ãã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã§æä¾ããããã¹ã¦ã®ãããªã㯠ã¡ã½ãããã©ãã(ãã³ã¬ã¼ã)ãã¾ã。
å§è²ã¯、ç¹å®ã®ã¯ã©ã¹ãç¶æ¿ã§ããªãå ´åã«ç¹ã«ä¾¿å©ã§ã。ã¯ã©ã¹å§è²ã使ãã¨、ã¯ã©ã¹ãä»ã®ã¯ã©ã¹ã®é層ã«å«ã¾ãããã¨ã¯ãªããªãã¾ã。ãã®ä»£ãã、åãã¤ã³ã¿ã¼ãã§ã¼ã¹ãå ±æã、å ã®åã®å é¨ãªãã¸ã§ã¯ãããã³ã¬ã¼ããã¾ã。ã¤ã¾ã、ãããªã㯠API ãç¶æããã¾ã¾、å®è£ ãç°¡åã«å ¥ãæ¿ãããã¨ãã§ãã¾ã。
ããããã£å§è²
by
ãã¼ã¯ã¼ãã使ãã¨、ã¯ã©ã¹å§è²ã ãã§ãªã、ããããã£ãå§è²ãããã¨ãã§ãã¾ã。ããããã£å§è²ã§ã¯、ããªã²ã¼ãã¯ããããã£ã® get
é¢æ°ã¨ set
é¢æ°ã®å¼ã³åºããæ
å½ãã¾ã。ä»ã®ãªãã¸ã§ã¯ã㧠getter/setter ãã¸ãã¯ãåå©ç¨ããªããã°ãªããªãå ´å、対å¿ãããã£ã¼ã«ãã ãã§ãªãæ©è½ãç°¡åã«æ¡å¼µãããã¨ãã§ããã®ã§、ãã®æ©è½ãé常ã«ä¾¿å©ã§ã。
次ã®ãããªå®ç¾©ã® Person
ã¯ã©ã¹ããã£ãã¨ãã¾ããã。
class Person(var name:String, var lastname:String)
ãã®ã¯ã©ã¹ã® name
ããããã£ã«ã¯、ããã¤ãã®ãã©ã¼ãããè¦ä»¶ãããã¾ã。name
ãè¨å®ããã¨ã、å
é ã®æåã大æå、ä»ã®æåãå°æåã«ãªãããã«ãã¾ã。ããã«、 name
ãæ´æ°ããå ´å、updateCount
ããããã£ãèªåçã«ã¤ã³ã¯ãªã¡ã³ããã¾ã。
ãã®æ©è½ã¯、次ã®ããã«å®è£ ãã¦ãããããããã¾ãã 。
<!-- Copyright 2019 Google LLC.SPDX-License-Identifier: Apache-2.0 -->
class Person(name: String, var lastname: String) {
var name: String = name
set(value) {
field = value.toLowerCase().capitalize()
updateCount++
}
var updateCount = 0
}
ããã¯åä½ãã¾ãã、è¦ä»¶ãå¤ãã£ã¦ lastname
ãå¤æ´ãããã¨ãã updateCount
ãã¤ã³ã¯ãªã¡ã³ããããã¨ã«ãªãã¨ã©ãã§ãããã。ãã¸ãã¯ãã³ãã¼ãã¦è²¼ãä»ã、ã«ã¹ã¿ã ã® setter ãæ¸ãã¦ãããããããã¾ããã、両æ¹ã®ããããã£ã«ã¾ã£ããåã setter ãæ¸ãã¦ãããã¨ã«æ°ã¥ãã§ããã。
<!-- Copyright 2019 Google LLC.SPDX-License-Identifier: Apache-2.0 -->
class Person(name: String, lastname: String) {
var name: String = name
set(value) {
field = value.toLowerCase().capitalize()
updateCount++
}
var lastname: String = lastname
set(value) {
field = value.toLowerCase().capitalize()
updateCount++
}
var updateCount = 0
}
ã©ã¡ãã® setter ã¡ã½ãããã»ã¼åãã¨ãããã¨ã¯、ã©ã¡ããã¯ä¸è¦ã¨ãããã¨ã§ã。ããããã£å§è²ã使ãã¨、getter 㨠setter ãããããã£ã«å§è²ãã¦ã³ã¼ããåå©ç¨ã§ãã¾ã。
ã¯ã©ã¹å§è²ã¨åãããã«、by
ã使ã£ã¦ããããã£ãå§è²ãã¾ã。ããã¨、ããããã£æ§æã使ã£ãã¨ãã«、Kotlin ã¯ããªã²ã¼ãã使ãã³ã¼ããçæãã¾ã。
<!-- Copyright 2019 Google LLC.SPDX-License-Identifier: Apache-2.0 -->
class Person(name: String, lastname: String) {
var name: String by FormatDelegate()
var lastname: String by FormatDelegate()
var updateCount = 0
}
ãã®å¤æ´ãè¡ãã¨、name
ããããã£ã¨ lastname
ããããã£ã FormatDelegate
ã¯ã©ã¹ã«å§è²ããã¾ã。FormatDelegate
ã®ã³ã¼ãã確èªãã¦ã¿ã¾ããã。ããªã²ã¼ã ã¯ã©ã¹ã¯、getter ã ããå§è²ããå ´å㯠ReadProperty<Any?, String>
ã、getter 㨠setter ã®ä¸¡æ¹ãå§è²ããå ´å㯠ReadWriteProperty<Any?, String>
ãå®è£
ããå¿
è¦ãããã¾ã。ãã®ä¾ã® FormatDelegate
ã¯、setter ãå¼ã³åºãããå ´åã«ãã©ã¼ãããå¦çãè¡ãã®ã§、ReadWriteProperty<Any?, String>
ãå®è£
ããªããã°ãªãã¾ãã。
<!-- Copyright 2019 Google LLC.SPDX-License-Identifier: Apache-2.0 -->
class FormatDelegate : ReadWriteProperty<Any?, String> {
private var formattedString: String = ""
override fun getValue(
thisRef: Any?,
property: KProperty<*>
): String {
return formattedString
}
override fun setValue(
thisRef: Any?,
property: KProperty<*>,
value: String
) {
formattedString = value.toLowerCase().capitalize()
}
}
getter é¢æ°ã¨ setter é¢æ°ã« 2 ã¤ã®è¿½å ãã©ã¡ã¼ã¿ããããã¨ã«æ°ã¥ããæ¹ãããã£ãããã§ããã。æåã®ãã©ã¡ã¼ã¿ thisRef
ã¯、ããããã£ãå«ããªãã¸ã§ã¯ãã表ãã¾ã。ããã使ãã¨、ãªãã¸ã§ã¯ãèªä½ã«ã¢ã¯ã»ã¹ã、ä»ã®ããããã£ã確èªããã、ä»ã®ã¯ã©ã¹é¢æ°ãå¼ã³åºãããã§ãã¾ã。2 ã¤ç®ã®ãã©ã¡ã¼ã¿ã¯ KProperty<*>
ã§ã。ããã¯、å§è²ãããããããã£ã«ã¤ãã¦ã®ã¡ã¿ãã¼ã¿ã«ã¢ã¯ã»ã¹ããããã«ä½¿ããã¨ãã§ãã¾ã。
å
ã»ã©ã®è¦ä»¶ãæãåºãã¦ã¿ã¦ãã ãã。thisRef
ã使ã£ã¦ updateCount
ããããã£ã«ã¢ã¯ã»ã¹ã、ã¤ã³ã¯ãªã¡ã³ããã¦ã¿ã¾ããã。
<!-- Copyright 2019 Google LLC.SPDX-License-Identifier: Apache-2.0 -->
override fun setValue(
thisRef: Any?,
property: KProperty<*>,
value: String
) {
if (thisRef is Person) {
thisRef.updateCount++
}
formattedString = value.toLowerCase().capitalize()
}
å é¨å¦ç
ãã®ä»çµã¿ãç解ãããã、éã³ã³ãã¤ã«ãã Java ã³ã¼ããè¦ã¦ã¿ã¾ã。Kotlin ã³ã³ãã¤ã©ã¯、name
ããããã£ã¨ lastname
ããããã£ã«ã¤ãã¦ã® FormatDelegate
ãªãã¸ã§ã¯ãã¸ã®ãã©ã¤ãã¼ããªåç
§ãä¿æããããã®ã³ã¼ãã¨、追å ãããã¸ãã¯ãå«ã getter/setter ã®ä¸¡æ¹ãçæãã¾ã。
ããã«、å§è²ãããããããã£ãä¿æãã KProperty[]
ãä½æãã¦ãã¾ã。name
ããããã£ã«å¯¾ãã¦çæããã getter 㨠setter ãè¦ã¦ã¿ãã¨、ã¤ã³ã¹ã¿ã³ã¹ã¯ã¤ã³ããã¯ã¹ 0 ã«ä¿åããã¦ãã¾ã。ä¸æ¹、lastname
ããããã£ã¯ã¤ã³ããã¯ã¹ 1 ã«ä¿åããã¦ãã¾ã。
public final class Person {// $FF: synthetic field
static final KProperty[] $$delegatedProperties = new KProperty[]{(KProperty)Reflection.mutableProperty1(new MutablePropertyReference1Impl(Reflection.getOrCreateKotlinClass(Person.class), "name", "getName()Ljava/lang/String;")), (KProperty)Reflection.mutableProperty1(new MutablePropertyReference1Impl(Reflection.getOrCreateKotlinClass(Person.class), "lastname", "getlastname()Ljava/lang/String;"))};
@NotNull
private final FormatDelegate name$delegate;
@NotNull
private final FormatDelegate lastname$delegate;
private int updateCount;
@NotNull
public final String getName() {
return this.name$delegate.getValue(this, $$delegatedProperties[0]);
}
public final void setName(@NotNull String var1) {
Intrinsics.checkParameterIsNotNull(var1, "<set-?>");
this.name$delegate.setValue(this, $$delegatedProperties[0], var1);
}
//...
}
ãã®ä»çµã¿ã«ãã£ã¦、é常ã®ããããã£æ§æã使ã£ã¦ä»»æã®å¼ã³åºãå ãå§è²ãããããããã£ã«ã¢ã¯ã»ã¹ã§ããããã«ãªã£ã¦ãã¾ã。
person.lastname = “Smith” //
println(“Update count is $person.count”)
Kotlin ã¯åã«å§è²ããµãã¼ããã¦ããã ãã§ã¯ããã¾ãã。Kotlin æ¨æºã©ã¤ãã©ãªã§çµã¿è¾¼ã¿ã®å§è²ãæä¾ãã¦ãã¾ãã、詳ããã¯å¥ã®è¨äºã§èª¬æãããã¨æãã¾ã。
å§è²ã¯ä»ã®ãªãã¸ã§ã¯ãã«ã¿ã¹ã¯ãå§è²ããéã«å½¹ç«ã¡、ã³ã¼ãã®åå©ç¨æ§ãé«ãã¾ã。Kotlin ã³ã³ãã¤ã©ã¯、å§è²ãã·ã¼ã ã¬ã¹ã«ä½¿ããããã«ã³ã¼ããä½æãã¾ã。Kotlin ã¯、by
ãã¼ã¯ã¼ãã使ã£ãã·ã³ãã«ãªæ§æã§ããããã£ãã¯ã©ã¹ã®å§è²ãè¡ãã¾ã。Kotlin ã³ã³ãã¤ã©ã¯、ãããªã㯠API ãä¸åå¤æ´ãã、å§è²ããµãã¼ãããããã«å¿
è¦ãªãã¹ã¦ã®ã³ã¼ããå
é¨çã«çæãã¾ã。ç°¡åã«è¨ãã°、Kotlin ã¯å§è²ã«å¿
è¦ãªãã¤ã©ã¼ãã¬ã¼ã ã³ã¼ãããã¹ã¦çæãã¦ç¶æãã¦ããã¾ã。ã¤ã¾ã、å§è²ã Kotlin ã«å§è²ãããã¨ãã§ããã®ã§ã。
Reviewed by Yuichi Araki - Developer Relations Team