You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classPerson(valname:String) {
funintroduceMyself() {
println("I am $name.")
}
}
classStudent(name:String, valid:Long) : Person(name) // Error: This type is final, so it cannot be inherited from
list 9.2 open を付けて 継承を行っている例
// superclass で `name` を プロパティとして定義openclassPerson(valname:String) {
funintroduceMyself() {
println("I am $name.")
}
}
// subclass で `name` を 改めて定義する必要がない// -> name には var(または val) を付けていないclassStudent(name:String, valid:Long) : Person(name)
funmain(args:Array<String>) {
val person:Person=Person("ゆたか")
person.introduceMyself() // I am ゆたか.val student:Student=Student("くみこ", 123)
println(student.id) // 123println(student.name) // くみこ
student.introduceMyself() // I am くみこ.
}
2. member の override
list 9.3 method の override
openclassPerson(valname:String) {
// `open`openfunintroduceMyself() {
println("I am $name.")
}
}
classStudent(name:String, valid:Long) : Person(name) {
// `override`overridefunintroduceMyself() {
println("I am $name(id=$id)")
}
}
funmain(args:Array<String>) {
val student:Student=Student("くみこ", 123)
student.introduceMyself() // I am くみこ(id=123)
}
packagesamplefunmain(args:Array<String>) {
publicFunction() // OK
privateFunction() // コンパイルエラー( Cannot access 'privateFunction': it is private in file )// 追記
internalFunction() // OK
}
class level における visibility modifier (可視性修飾子)
list 9.12 sample.kt
openclassFoo {
privatefunprivateMethod() {}
protectedfunprotectedMethod() {}
}
classBar : Foo() {
funexecute() {
privateMethod() // コンパイルエラー (Cannot access 'privateMethod': it is invisible (private in a supertype) in 'Bar')
protectedMethod() // subclass なので OK
}
}
classBaz {
funexecute(foo:Foo) {
foo.protectedMethod() // コンパイルエラー (Cannot access 'protectedMethod': it is protected in 'Foo')
}
}