Created
September 13, 2017 00:26
-
-
Save tikidunpon/10b771570d2cb957bfe7b950f512e758 to your computer and use it in GitHub Desktop.
#read_kotlin Kotlinスタートブック読書勉強会 #5 第7,8章 クラスとそのメンバ〜(参考資料)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by koichitanaka on 2017/06/11. | |
*/ | |
fun <T> println(message: T) { | |
print(Thread.currentThread().stackTrace[2].lineNumber.toString() + "行目 ") | |
print(message) | |
print("\n") | |
} | |
fun main(args: Array<String>) { | |
//p99 オブジェクトからクラスへ | |
// p100 バケツオブジェクトの生成(オブジェクト式) | |
val bucket = object { | |
// バケツの容量 | |
val capacity: Int = 5 | |
// 入っている水の量 | |
var quantity: Int = 0 | |
// バケツを水で満たす | |
fun fill() { | |
quantity = capacity | |
} | |
// 排水する | |
fun drainAway() { | |
quantity = 0 | |
} | |
// 入っている水の量を出力する | |
fun printQuantity() { | |
println(quantity) | |
} | |
} | |
bucket.printQuantity() // 0と出力される | |
bucket.fill() | |
bucket.printQuantity() // 5と出力される | |
bucket.drainAway() | |
bucket.printQuantity() // 0と出力される | |
} | |
/* | |
interface Bucket | |
fun main(args: Array<String>) { | |
// p102 インターフェース | |
val bucket = object : Bucket { | |
// バケツの容量 | |
val capacity: Int = 5 | |
// 入っている水の量 | |
var quantity: Int = 0 | |
// バケツを水で満たす | |
fun fill() { | |
quantity = capacity | |
} | |
// 排水する | |
fun drainAway() { | |
quantity = 0 | |
} | |
// 入っている水の量を出力する | |
fun printQuantity() { | |
println(quantity) | |
} | |
// 別のバケツに水を注ぐ | |
fun pourTo(that: Bucket) { | |
// 未実装 | |
} | |
} | |
bucket.printQuantity() // 0と出力される | |
bucket.fill() | |
bucket.printQuantity() // 5と出力される | |
bucket.drainAway() | |
bucket.printQuantity() // 0と出力される | |
} | |
*/ | |
/* | |
// p104 7.3 提供スべき関数を定めたバケツインターフェース | |
interface Bucket { | |
fun fill() | |
fun drainAway() | |
fun pourTo(that: Bucket) | |
// Javaで一般的なゲッター/セッター | |
fun getCapacity(): Int | |
fun getQuantity(): Int | |
fun setQuantity(quantity: Int) | |
} | |
// p104 7.4 バケツオブジェクトを生成する関数 | |
fun createBucket(capacity: Int): Bucket = object : Bucket { | |
var _quantity: Int = 0 | |
override fun fill() { | |
setQuantity(getCapacity()) | |
} | |
override fun drainAway() { | |
setQuantity(0) | |
} | |
override fun pourTo(that: Bucket) { | |
val thatVacuity = that.getCapacity() - that.getQuantity() | |
if (getQuantity() <= thatVacuity) { | |
that.setQuantity(that.getQuantity() + getQuantity()) | |
drainAway() | |
} else { | |
that.fill() | |
setQuantity(getQuantity() - thatVacuity) | |
} | |
} | |
override fun getCapacity(): Int = capacity | |
override fun getQuantity(): Int = _quantity | |
override fun setQuantity(quantity: Int) { | |
_quantity = quantity | |
} | |
} | |
fun main(args: Array<String>) { | |
// 容量7のバケツを作る | |
val bucket1 = createBucket(7) | |
// 容量4のバケツを作る | |
val bucket2 = createBucket(4) | |
// バケツ1を満たす | |
bucket1.fill() | |
// バケツ1からバケツ2に可能な限り注ぐ | |
bucket1.pourTo(bucket2) | |
println(bucket1.getQuantity()) // 3を出力 | |
println(bucket2.getQuantity()) // 4を出力 | |
} | |
*/ | |
/* | |
// p107 プロパティを持ったインターフェース | |
interface Bucket { | |
fun fill() | |
fun drainAway() | |
fun pourTo(that: Bucket) | |
// プロパティ | |
val capacity: Int | |
var quantity: Int | |
} | |
// p108 7.7 プロパティをオーバーライドするバケツ | |
fun createBucket(_capacity: Int): Bucket = object : Bucket { | |
override val capacity: Int = _capacity | |
override var quantity: Int = 0 | |
override fun fill() { | |
quantity = capacity | |
} | |
override fun drainAway() { | |
quantity = 0 | |
} | |
override fun pourTo(that: Bucket) { | |
val thatVacuity = that.capacity - that.quantity | |
if (capacity <= thatVacuity) { | |
that.quantity += quantity | |
drainAway() | |
} else { | |
that.fill() | |
quantity -= thatVacuity | |
} | |
} | |
} | |
*/ | |
/* | |
// p109 | |
interface Bucket { | |
fun fill() | |
fun drainAway() | |
fun pourTo(that: Bucket) | |
// プロパティ | |
val capacity: Int | |
var quantity: Int | |
} | |
// p109 クラスBucketImpl | |
class BucketImpl(_capacity: Int) : Bucket { | |
override val capacity: Int = _capacity | |
override var quantity: Int = 0 | |
override fun fill() { | |
quantity = capacity | |
} | |
override fun drainAway() { | |
quantity = 0 | |
} | |
override fun pourTo(that: Bucket) { | |
val thatVacuity = that.capacity - that.quantity | |
if (capacity <= thatVacuity) { | |
that.quantity += quantity | |
drainAway() | |
} else { | |
that.fill() | |
quantity -= thatVacuity | |
} | |
} | |
} | |
// p110 7.9 インスタンスの生成 | |
fun main(args: Array<String>) { | |
val bucket1: Bucket = BucketImpl(7) | |
val bucket2: Bucket = BucketImpl(4) | |
bucket1.fill() | |
bucket1.pourTo(bucket2) | |
println(bucket1.quantity) // 3を出力 | |
println(bucket2.quantity) // 4を出力 | |
} | |
*/ | |
// p113 8.1 もっとも単純なクラス定義 | |
/* | |
class MyClass | |
// p114 MyClassインスタンスの生成 | |
fun main(args: Array<String>) { | |
val myClass: MyClass = MyClass() | |
println(myClass) | |
} | |
*/ | |
// p115 メソッドの例 | |
/* | |
class Greeter { | |
fun greet(name: String) { | |
println("Hello, $name!") | |
} | |
} | |
fun main(args: Array<String>) { | |
val greeter = Greeter() | |
greeter.greet("Kotlin") // Hello Kotlin!と出力 | |
} | |
*/ | |
// p116 プロパティ | |
// p116 8.4 クラスPerson | |
/* | |
class Person { | |
var name: String = "" | |
var age: Int = 0 | |
} | |
// p116 8.5 プロパティ使用の例 | |
fun main(args: Array<String>) { | |
val hanako = Person() | |
println(hanako.name) // 空文字を出力 | |
println(hanako.age) // 0を出力 | |
hanako.name = "はなこ" | |
hanako.age = 25 | |
println(hanako.name) // はなこを出力 | |
println(hanako.age) // 25を出力 | |
} | |
*/ | |
// p117 8.6 バッキングフィールドを持たないプロパティ nameLength | |
/* | |
class Person { | |
var name: String = "" | |
var age: Int = 0 | |
val nameLength: Int | |
get(): Int { | |
return this.name.length | |
} | |
// p117 8.7 カスタムゲッターの省略記法 | |
// val nameLength: Int | |
// get() = this.name.length | |
} | |
fun main(args: Array<String>) { | |
val hanako = Person() | |
hanako.name = "はなこ" | |
println(hanako.nameLength) // 3を出力 | |
} | |
*/ | |
// p118 8.8 カスタムセッターの例 | |
/* | |
class Person { | |
var name: String = "" | |
set(value) { | |
println("$(value)がセットされました") | |
field = value | |
} | |
var age: Int = 0 | |
val nameLength: Int | |
get() = this.name.length | |
} | |
fun main(args: Array<String>) { | |
val hanako = Person() | |
hanako.name = "はなこ" // はなこがセットされましたされました を出力 | |
println(hanako.name) // はなこを出力 | |
} | |
*/ | |
// p119 遅い初期化 | |
// p119 lateinitを付けたプロパティ | |
/* | |
class MyClass { | |
lateinit var foo: String | |
} | |
fun main(args: Array<String>) { | |
val m = MyClass() | |
// kotlin.UninitializedPropertyAccessException: lateinit property foo has not been initialize | |
m.foo | |
} | |
*/ | |
/* | |
// p121 コンストラクタとイニシャライザ | |
// p121 8.11 引数を取るコンストラクタ | |
class Rational constructor(n: Int, d: Int) { | |
val numerator: Int = n | |
val denominator: Int = d | |
} | |
// p121 8.11 引数をそのままプロパティに | |
//class Rational(val numerator: Int, val denominator: Int) | |
fun main(args: Array<String>) { | |
val half = Rational(1, 2) | |
println(half.numerator) // 1を出力 | |
println(half.denominator) //2を出力 | |
} | |
*/ | |
/* | |
// p122 セカンダリコンストラクタ | |
class Rational(val numerator: Int, val denominator: Int) { | |
constructor(numerator: Int) : this(numerator, 1) | |
} | |
// p122 8.13 コンストラクタ引数にもデフォルト値を設定可能 | |
//class Rational(val numerator: Int, val denominator: Int = 1) | |
fun main(args: Array<String>) { | |
val half = Rational(1, 2) | |
println("${half.numerator}/${half.denominator}") //1/2を出力 | |
val five = Rational(5) | |
println("${five.numerator}/${five.denominator}") //5/1を出力 | |
} | |
*/ | |
// p123 イニシャライザ | |
// p123 8.14 イニシャライザの例 | |
/* | |
class Rational(val numerator: Int, val denominator: Int = 1) { | |
init { | |
// 要求に反した場合、例外をスローする標準ライブラリの関数 | |
require(denominator != 0) | |
} | |
} | |
fun main(args: Array<String>) { | |
Rational(1, 1) //OK | |
Rational(1, 0) // java.lang.IllegalArgumentException: Failed requirement | |
} | |
*/ | |
// p124 エクステンション | |
// p124 8.15 Stringを引数にとる関数 | |
/* | |
fun countWords(s: String): Int = | |
s.split("""\s+""".toRegex()).size | |
// p124 8.16 Stringを引数にとる関数の呼び出し例 | |
fun main(args: Array<String>) { | |
countWords("I like Kotlin") //3 | |
} | |
*/ | |
// p125 8.18 Stringに対する拡張関数 | |
/* | |
fun String.countWords(): Int = | |
this.split("""\s""".toRegex()).size | |
val String.wordCount2: Int | |
get() = split("""\s+""".toRegex()).size | |
// p124 8.17 Stringに対する拡張関数の呼び出し例 | |
fun main(args: Array<String>) { | |
"I like Kotlin".countWords() | |
println("I like Kotlin".wordCount2) | |
} | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment