{"id":3919,"date":"2015-11-06T10:02:58","date_gmt":"2015-11-06T01:02:58","guid":{"rendered":"http:\/\/www.lancard.com\/blog\/?p=3919"},"modified":"2025-03-12T11:28:05","modified_gmt":"2025-03-12T02:28:05","slug":"useful-kotlin-3","status":"publish","type":"post","link":"https:\/\/www.lancard.com\/blog\/2015\/11\/06\/useful-kotlin-3\/","title":{"rendered":"Kotlin \u306e\u4fbf\u5229\u306a\u6a5f\u80fd 3 \u9078"},"content":{"rendered":"<p>\u4e3b\u306b Android \u306e\u754c\u9688\u3067\u6700\u8fd1\u306b\u308f\u304b\u306b\u53d6\u308a\u4e0a\u3052\u3089\u308c\u3066\u3044\u308b Kotlin \u3067\u3059\u304c\u3001\u3057\u3070\u3089\u304f\u4f7f\u3063\u3066\u307f\u3066\u4fbf\u5229\u3060\u3068\u611f\u3058\u3066\u3044\u308b\u6a5f\u80fd\u3092 3 \u3064\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p><!--more--><\/p>\n<h1>\u30e9\u30e0\u30c0\u5f0f\uff08SAM \u5909\u63db\uff09<\/h1>\n<p>\u79c1\u304c Kotlin \u3092\u8a66\u3059\u304d\u3063\u304b\u3051\u306b\u306a\u3063\u305f\u6a5f\u80fd\u3067\u3059\u3002Kotlin \u306b\u306f\u30e9\u30e0\u30c0\u5f0f\u304c\u3042\u308a\u3001\u30a4\u30d9\u30f3\u30c8\u30ea\u30b9\u30ca\u30fc\u306e\u3088\u3046\u306a Java \u3067\u30e1\u30bd\u30c3\u30c9\u3092\u3072\u3068\u3064\u6301\u3064\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\uff08Single Abstract Method \u3068\u8868\u73fe\u3057\u305f\u308a\u3059\u308b\uff09\u3092\u7c21\u6f54\u306b\u8a18\u8f09\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/p>\n<pre lang=\"java\">\ntextView.setOnClickListener(new View.OnClickListener() {\n    @Override\n    public void onClick(View v) {\n        Toast.makeText(context,\n                textView.getText(),\n                Toast.LENGTH_SHORT).show();\n    }\n});\n<\/pre>\n<p>\u3053\u3046\u3044\u3046\u306e\u304c<\/p>\n<pre lang=\"kotlin\">\ntextView.setOnClickListener { view ->\n    Toast.makeText(this,\n            textView.text,\n            Toast.LENGTH_SHORT).show()\n}\n<\/pre>\n<p>\u3053\u3046\u3044\u3046\u611f\u3058\u3067\u30b7\u30f3\u30d7\u30eb\u306b\u66f8\u3051\u307e\u3059\u3002<a href=\"http:\/\/qiita.com\/RyotaMurohoshi\/items\/01b370f34a4bf96f5c39\">\u3053\u3061\u3089\u306e\u8a18\u4e8b<\/a>\u306a\u3069\u5206\u304b\u308a\u6613\u304f\u3066\u53c2\u8003\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<h1>\u9ad8\u968e\u95a2\u6570\u3001\u30ea\u30b9\u30c8\u51e6\u7406<\/h1>\n<p>Kotlin \u4f7f\u3044\u521d\u3081\u306e\u9803\u306f Java \u306e\u611f\u899a\u3067\u30ea\u30b9\u30c8\u51e6\u7406\u3092\u62e1\u5f35 for \u6587\u3067\u3084\u3063\u3066\u3044\u307e\u3057\u305f\u304c\u3001Kotlin \u306f\u9ad8\u968e\u95a2\u6570\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u304a\u308a\u3001\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u306f\u30ea\u30b9\u30c8\u51e6\u7406\u306e\u95a2\u6570\u3082\u3042\u308a\u307e\u3059\u3002\u30e9\u30e0\u30c0\u5f0f\u304c\u4f7f\u3048\u308b\u6642\u70b9\u3067\u6c17\u4ed8\u304f\u3079\u304d\u3060\u3063\u305f\u2026\u3002<\/p>\n<pre lang=\"kotlin\">\narrayListOf(1, 2, 3).map { i -> i * 2 }\n\narrayListOf(1, 2, 3).map { it * 2 }\n\nfun twice(i: Int): Int { return i * 2}\narrayListOf(1, 2, 3).map { ::twice }\n\n\/\/ \u3044\u305a\u308c\u3082 [2, 4, 6] \u306e ArrayList \u3092\u8fd4\u3059\n<\/pre>\n<p>\u30e9\u30e0\u30c0\u5f0f\u306e\u5f15\u6570\u304c 1 \u3064\u306e\u5834\u5408\u306f\u30012 \u3064\u76ee\u306e\u4f8b\u306e\u3088\u3046\u306b\u6697\u9ed9\u306b <code>it<\/code> \u3068\u3044\u3046\u540d\u524d\u3067\u5f15\u6570\u3092\u5b9a\u7fa9\u3057\u3066\u304f\u308c\u307e\u3059\u3002<br \/>\n\u307e\u305f 3 \u3064\u76ee\u306e\u4f8b\u306e\u3088\u3046\u306b\u95a2\u6570\u53c2\u7167\u3092\u6e21\u3059\u3053\u3068\u3082\u51fa\u6765\u307e\u3059\u3002<\/p>\n<p>\u95a2\u6570\u8133\u304c\u5f31\u3044\u306e\u3067\u9ad8\u968e\u95a2\u6570\u306f\u307e\u3060\u307e\u3060\u4f7f\u3044\u719f\u305b\u3066\u306a\u304f\u3001\u3072\u3068\u307e\u305a\u30ea\u30b9\u30c8\u51e6\u7406\u304b\u3089\u3067\u3082\u521d\u3081\u3066\u52c9\u5f37\u3057\u3066\u3044\u304f\u6240\u5b58\u3067\u3059\u3002<\/p>\n<p><a href=\"https:\/\/sites.google.com\/site\/tarokotlin\/chap6\/sec62\">6.2 \u30ea\u30b9\u30c8 &#8211; \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9eKotlin \u89e3\u8aac<\/a><br \/>\n<a href=\"https:\/\/kotlinlang.org\/docs\/reference\/lambdas.html#function-literals-and-function-expressions\">Higher-Order Functions and Lambdas<\/a><\/p>\n<h1>\u30c7\u30fc\u30bf\u30af\u30e9\u30b9<\/h1>\n<p>Kotlin \u306b\u306f\u30c7\u30fc\u30bf\u30af\u30e9\u30b9\u3092\u7c21\u5358\u306b\u5b9a\u7fa9\u51fa\u6765\u308b data \u4fee\u98fe\u5b50\u304c\u3042\u308a\u307e\u3059\u3002lombok \u306e <code>@data<\/code> \u30a2\u30ce\u30c6\u30fc\u30b7\u30e7\u30f3\u306b\u8fd1\u3044\u611f\u899a\u3067\u3059\u306d\u3002<\/p>\n<pre lang=\"kotlin\">\ndata class Data(\n        var nickname: String,\n        val status: String = \"ok\",\n        val code: String,\n)\n\nval data1 = Data(\"foo\", \"ok\", \"code1\")\nval data2 = Data(nickname = \"bar\", code = \"code2\")\n<\/pre>\n<p>\u30af\u30e9\u30b9\u672c\u4f53\u306e\u8a18\u8ff0\u306f\u7701\u7565\u53ef\u80fd\u306a\u306e\u3067\u3053\u308c\u3060\u3051\u3067\u3059\u3002\u30c7\u30fc\u30bf\u30af\u30e9\u30b9\u306b\u306f <code>equals()<\/code>\/<code>hashCode()<\/code>\/<code>toString()<\/code>\/<code>copy()<\/code> \u304c\u81ea\u52d5\u7684\u306b\u5b9a\u7fa9\u3055\u308c\u307e\u3059\u3002<br \/>\n\u30c7\u30fc\u30bf\u30af\u30e9\u30b9\u306e\u6a5f\u80fd\u3067\u306f\u7121\u3044\u3067\u3059\u304c\u3001Kotlin \u306f\u30c7\u30d5\u30a9\u30eb\u30c8\u5f15\u6570\u3084\u540d\u524d\u4ed8\u304d\u5f15\u6570\u3082\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u308b\u306e\u3067\u521d\u671f\u5316\u6642\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3082\u7701\u7565\u51fa\u6765\u307e\u3059\u3002<\/p>\n<h1>\u6240\u611f<\/h1>\n<p>Ruby \u306e\u3088\u3046\u306a\u8a00\u8a9e\u3060\u3068\u9ad8\u968e\u95a2\u6570\u3084\u30e9\u30e0\u30c0\uff08\u30d6\u30ed\u30c3\u30af\uff09\u306f\u3088\u304f\u3042\u308b\u6a5f\u80fd\u3067\u3059\u304c\u3001\u3053\u308c\u3092 Java \u304c\u5fc5\u8981\u306a\u30b7\u30c1\u30e5\u30a8\u30fc\u30b7\u30e7\u30f3\u3067\u3082\u4f7f\u3063\u3066\u3044\u3051\u308b\u306e\u306f\u304b\u306a\u308a\u3042\u308a\u304c\u305f\u3044\u3067\u3059\u3002Kotlin \u306f Java \u3068\u306e\u89aa\u548c\u6027\u3092\u610f\u8b58\u3057\u3066\u3044\u308b\u70ba\u3001\u65e2\u5b58\u306e Java \u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u3088\u3046\u306a\u8cc7\u7523\u3092\u82e6\u7121\u304f\u4f7f\u3048\u308b\u5834\u5408\u304c\u591a\u3044\u306e\u3082\u5f37\u307f\u3067\u3059\u306d\u3002<\/p>\n<p>\u4ed6\u306b\u3082 Java \u30b3\u30fc\u30c9\u3067\u66f8\u304b\u308c\u305f\u30af\u30e9\u30b9\u306e getter\/setter \u3092\u30d7\u30ed\u30d1\u30c6\u30a3\u3088\u308d\u3057\u304f\u6271\u3046\u3053\u3068\u304c\u51fa\u6765\u305f\u308a\u3001\u3069\u3093\u3069\u3093\u4f7f\u3044\u6613\u304f\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u5272\u3068 Android \u76ee\u7dda\u306b\u5bc4\u3063\u305f\u5185\u5bb9\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u304c\u3001\u3082\u3063\u3068\u4fbf\u5229\u306a\u6a5f\u80fd\u306f\u305f\u304f\u3055\u3093\u3042\u308b\u306f\u305a\u306a\u306e\u3067\u662f\u975e<a href=\"https:\/\/kotlinlang.org\/docs\/reference\/\">\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9<\/a>\u3082\u898b\u3066\u307f\u3066\u4e0b\u3055\u3044\u3002\u30d5\u30eb PDF \u3084<a href=\"http:\/\/try.kotlinlang.org\/\">\u30aa\u30f3\u30e9\u30a4\u30f3\u5b9f\u884c\u74b0\u5883<\/a>\u3082\u7f6e\u3044\u3066\u3042\u308a\u307e\u3059\u3002<br \/>\n1.0 \u306e\u30ea\u30ea\u30fc\u30b9\u3082\u8fd1\u4ed8\u3044\u3066\u304d\u3066\u3044\u308b\u3088\u3046\u3067\u3053\u308c\u304b\u3089\u3082\u76ee\u304c\u96e2\u305b\u307e\u305b\u3093\u3002<\/p>\n<hr \/>\n<p>\u3053\u306e\u8a18\u4e8b\u306f\u696d\u52d9\u6642\u9593\u306e\u3046\u3061 20% \u306e\u6357\u3089\u306a\u3044\u6642\u9593\u3092\u5229\u7528\u3057\u3066\u66f8\u304b\u308c\u307e\u3057\u305f\u3002\u9032\u6357\u30c0\u30e1\u3067\u3059\u2026\u3002<\/p>\n<a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-facebook nolightbox\" data-provider=\"facebook\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Facebook\" href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F3919&amp;t=Kotlin%20%E3%81%AE%E4%BE%BF%E5%88%A9%E3%81%AA%E6%A9%9F%E8%83%BD%203%20%E9%81%B8&amp;s=100&amp;p[url]=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F3919&amp;p[images][0]=&amp;p[title]=Kotlin%20%E3%81%AE%E4%BE%BF%E5%88%A9%E3%81%AA%E6%A9%9F%E8%83%BD%203%20%E9%81%B8\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"Facebook\" title=\"Share on Facebook\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/facebook.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-twitter nolightbox\" data-provider=\"twitter\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Twitter\" href=\"http:\/\/twitter.com\/share?url=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F3919&amp;text=Kotlin%20%E3%81%AE%E4%BE%BF%E5%88%A9%E3%81%AA%E6%A9%9F%E8%83%BD%203%20%E9%81%B8\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"twitter\" title=\"Share on Twitter\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/twitter.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-linkedin nolightbox\" data-provider=\"linkedin\" target=\"_blank\" rel=\"nofollow\" title=\"Share on Linkedin\" href=\"https:\/\/www.linkedin.com\/shareArticle?mini=true&amp;url=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F3919&amp;title=Kotlin%20%E3%81%AE%E4%BE%BF%E5%88%A9%E3%81%AA%E6%A9%9F%E8%83%BD%203%20%E9%81%B8\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"linkedin\" title=\"Share on Linkedin\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/linkedin.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-tumblr nolightbox\" data-provider=\"tumblr\" target=\"_blank\" rel=\"nofollow\" title=\"Share on tumblr\" href=\"https:\/\/tumblr.com\/share?s=&amp;v=3&amp;t=Kotlin%20%E3%81%AE%E4%BE%BF%E5%88%A9%E3%81%AA%E6%A9%9F%E8%83%BD%203%20%E9%81%B8&amp;u=https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F3919\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px;margin-right:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"tumblr\" title=\"Share on tumblr\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/tumblr.png\" \/><\/a><a class=\"synved-social-button synved-social-button-share synved-social-size-24 synved-social-resolution-single synved-social-provider-mail nolightbox\" data-provider=\"mail\" rel=\"nofollow\" title=\"Share by email\" href=\"mailto:?subject=Kotlin%20%E3%81%AE%E4%BE%BF%E5%88%A9%E3%81%AA%E6%A9%9F%E8%83%BD%203%20%E9%81%B8&amp;body=%E3%82%B7%E3%82%A7%E3%82%A2%E3%81%99%E3%82%8B%EF%BC%9A:%20https%3A%2F%2Fwww.lancard.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F3919\" style=\"font-size: 0px;width:24px;height:24px;margin:0;margin-bottom:5px\"><img loading=\"lazy\" decoding=\"async\" alt=\"mail\" title=\"Share by email\" class=\"synved-share-image synved-social-image synved-social-image-share\" width=\"24\" height=\"24\" style=\"display: inline;width:24px;height:24px;margin: 0;padding: 0;border: none;box-shadow: none\" src=\"https:\/\/www.lancard.com\/blog\/wp-content\/plugins\/social-media-feather\/synved-social\/image\/social\/regular\/48x48\/mail.png\" \/><\/a>","protected":false},"excerpt":{"rendered":"<p>\u4e3b\u306b Android \u306e\u754c\u9688\u3067\u6700\u8fd1\u306b\u308f\u304b\u306b\u53d6\u308a\u4e0a\u3052\u3089\u308c\u3066\u3044\u308b Kotlin \u3067\u3059\u304c\u3001\u3057\u3070\u3089\u304f\u4f7f\u3063\u3066\u307f\u3066\u4fbf\u5229\u3060\u3068\u611f\u3058\u3066\u3044\u308b\u6a5f\u80fd\u3092 3 \u3064\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"author":27,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93,145],"tags":[],"class_list":["post-3919","post","type-post","status-publish","format-standard","hentry","category-android","category-kotlin"],"_links":{"self":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts\/3919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/comments?post=3919"}],"version-history":[{"count":9,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts\/3919\/revisions"}],"predecessor-version":[{"id":3932,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/posts\/3919\/revisions\/3932"}],"wp:attachment":[{"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/media?parent=3919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/categories?post=3919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lancard.com\/blog\/wp-json\/wp\/v2\/tags?post=3919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}