当前位置:网站首页>Kotlin reflection -- Notes
Kotlin reflection -- Notes
2022-06-25 06:14:00 【NO Exception?】
1. Class reference
kotlin Is based on java1.6 Design , Fully compatible with java, So and java Many functions are interconnected . Such as java In reflection Class object , stay Kotlin It's called KClass object .
1.1 Class and KClass
Class
// stay kotlin In order to get Class object
class ReflectDemo(val x: Int = 0) {
constructor() : this(0) {
}
fun test() {
println(x)
}
}
// obtain Class Two methods of object instances
val class1 = ReflectDemo().javaClass
val class2: Class<ReflectDemo> = ReflectDemo::class.java
KClass
// obtain KClass Object instance method
val kClass1 = ReflectDemo::class
val kClass2 = ReflectDemo().javaClass.kotlin
1.2 Get instance object
val instance = class1.newInstance()
instance.x
instance.test()
val instance2 = class2.newInstance()
instance2.x
instance2.test()
//kotlin Get instance object
kClass1.objectInstance
1.3 Access method
// Get all methods containing parent classes
kclass.memberFunctions
// Get all the methods of the current class
kclass.declaredMemberFunctions
for (item in kclass.declaredMemberFunctions) {
println("${
item.name}")
// Calling method
// item.call(reflectDemo)
}
1.4 get attribute
val reflectDemo = ReflectDemo(33)
val kclass = ReflectDemo::class
//kclass.members All properties of the current class and parent class
//kclass.memberProperties Print all properties of the current class
for (item in kclass.memberProperties) {
val obj = item as KProperty<*>
println("${
obj.name}:${
obj.call(reflectDemo)}")
}
1.5 Get comments
2. other
1.kotlin Object to map
fun <A : Any> toMap(a: A): Map<String, Any?> {
// Traverse a All attributes of ,name to value
return a::class.members.map {
m ->
val p = m as KProperty
p.name to p.call(a)
}.toMap()
}
边栏推荐
- D compile time reflection
- C simple operation mongodb
- JS implementation mouse can achieve the effect of left and right scrolling
- Rhcsa day 4
- Three tier architecture experiment
- Copying DNA
- Use generator-easy-ui5 to quickly create the engineering structure of SAP ui5 applications
- Day21 performance test process
- The e-book "action guide for large organizations to further promote zero code application platform" was officially released!
- Research Report on brand strategic management and marketing trends in the global and Chinese preserved fruit market 2022
猜你喜欢

Mongodb basic concept learning - Documentation
How SAP ui5 device type detection device API works

Digitalization, transformation?

What happens when redis runs out of memory
Technology Review: what is the evolution route of container technology? What imagination space is there in the future?
Kubevela v1.2 release: the graphical operation console velaux you want is finally here!

Distributed solar photovoltaic inverter monitoring
![[road of system analyst] collection of wrong questions in the chapters of Applied Mathematics and economic management](/img/62/dab2ac0526795f2040394acd9efdd3.jpg)
[road of system analyst] collection of wrong questions in the chapters of Applied Mathematics and economic management
SAP Fiori tools and corresponding cli (command line interface)

Location object
随机推荐
SAP ui5 tutorial for beginners part XXVI - detailed steps for using OData service with mock server trial version
The sum problem
C simple operation mongodb
证券如何在线开户?在线开户是安全么?
Rational investment and internationalism
Lesson 9: workspace introduction
HashSet implementation class
50 days countdown! Are you ready for the Landbridge cup provincial tournament?
Global and China financial guarantee marketing strategy and channel dynamic construction report 2022
Noi Mathematics: Dirichlet convolution
Part 33 of SAP ui5 application development tutorial - trial version of responsiveness of SAP ui5 applications
Soft exam information system project manager_ Management Science (Operations Research) -- senior information system project manager of soft test 033
[Suanli network] problems and challenges faced by the development of Suanli network
Go quiz: considerations for function naming return value from the go interview question (more than 80% of people answered wrong)
China rehabilitation hospital industry operation benefit analysis and operation situation investigation report 2022
Use of collection
Global and Chinese medical protective clothing market supply and demand research and investment value proposal report 2022-2028
Part 34 of SAP ui5 application development tutorial - device adaptation of SAP ui5 application based on device type
Gavin's insight on transformer live class - line by line analysis and field experiment analysis of insurance BOT microservice code of insurance industry in the actual combat of Rasa dialogue robot pro
IQ debugging of Hisilicon platform ISP and image (1)