- Learn Kotlin Programming(Second Edition)
- Stephen Samuel Stefan Bocutiu
- 366字
- 2021-06-24 14:13:33
Visibility modifiers
When you define your class, contained methods, properties, or fields can have various visibility levels. In Kotlin, there are four possible values:
- Public: This can be accessed from anywhere
- Internal: This can only be accessed from the module code
- Protected: This can only be accessed from the class defining it and any derived classes
- Private: This can only be accessed from the scope of the class defining it
If the parent class specifies that a given field is open for being redefined (overwritten), the derived class will be able to modify the visibility level. Here is an example:
open class Container { protected open val fieldA: String = "Some value" } class DerivedContainer : Container() { public override val fieldA: String = "Something else" }
Now in the main class, you can create a new DerivedContainer instance and print out the value of fieldA. Yes, this field is now public to any code:
val derivedContainer = DerivedContainer() println("DerivedContainer.fieldA:${derivedContainer.fieldA}") /*val container:Container=derivedContainer
println("fieldA:${container.fieldA}")*/
I commented out the code where we use derivedContainer as if it was an instance of Container. If having an instance of derived conatainer is the case, trying to compile the commented code will yield an error because fieldA is not accessible.
Redefining the field doesn't mean it will replace the existing one when it comes to object allocation. Remember, a derived class inherits all the parent class fields. It takes just a little bit of code to prove this:
derivedContainer.javaClass.superclass.getDeclaredFields().forEach { field-> field.setAccessible(true) println("Field:${field.name},${Modifier.toString(field.modifiers)} , Value=${field.get(derivedContainer)}") } derivedContainer.javaClass.getDeclaredFields().forEach { field-> field.setAccessible(true) println("Field:${field.name},${Modifier.toString(field.modifiers)} , Value=${field.get(derivedContainer)}") }
Run the preceding code and it will print fieldA twice in the output; the first entry will come from the parent class and will be Some Value, and the latter will come from the derived class and will read Something else.
A typical use case would be to widen the access for a given field, method, and/or property. But you should be careful about using this since it might break the Liskov substitution principle. Following this principle, if a program is using a base class, then the reference to the base class can be replaced with a derived class without affecting the functionality of the program.
- Clojure Programming Cookbook
- C語(yǔ)言程序設(shè)計(jì)基礎(chǔ)與實(shí)驗(yàn)指導(dǎo)
- AIRAndroid應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)
- 編寫整潔的Python代碼(第2版)
- PHP網(wǎng)絡(luò)編程學(xué)習(xí)筆記
- Python忍者秘籍
- Visual Basic程序設(shè)計(jì)實(shí)踐教程
- Mastering Xamarin.Forms(Second Edition)
- Mastering AWS Security
- Extending Unity with Editor Scripting
- After Effects CC案例設(shè)計(jì)與經(jīng)典插件(視頻教學(xué)版)
- Ionic3與CodePush初探:支持跨平臺(tái)與熱更新的App開(kāi)發(fā)技術(shù)
- Java EE程序設(shè)計(jì)與開(kāi)發(fā)實(shí)踐教程
- 小學(xué)生C++趣味編程從入門到精通
- JavaScript高級(jí)程序設(shè)計(jì)(第4版)