当前位置:网站首页>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()
}
边栏推荐
- [Suanli network] problems and challenges faced by the development of Suanli network
- MySQL tuning --01--- optimization steps and system performance parameters
- An example of recursion, the full permutation problem of 1~n
- Global and Chinese gallium nitride (GAN) market output value scale forecast and application prospect analysis report 2022
- Netstat command – displays network status
- Interviewer: what is an iterator? What is the relationship between async await and iterators?
- Research Report on marketing channel analysis and competitive strategy of China's polycarbonate industry 2022
- Processes and threads - concepts and process scheduling
- What is SAP sup - Sybase unwired platform
- SAP ui5 beginner tutorial No. 28 - Introduction to the integration test tool OPA for SAP ui5 applications
猜你喜欢

Tablespace free space

Getting started with mongodb
How the sap ui5 framework performs single step debugging of batch requests
SAP ui5 application development tutorial 32 - how to create a custom SAP ui5 control

MySQL tuning -- 02 -- slow query log

MySQL uses the where condition to find strange results: solve

Uni app wechat applet customer service chat function

Lesson 9: workspace introduction
Configuration file ui5 local in SAP ui5 tools Configuration points of yaml

【LeetCode】40. Combined summation II (2 strokes of wrong questions)
随机推荐
Leetcode sword finger offer question brushing - day 27
Fdisk command – disk partition
RM command – remove file or directory
Part 33 of SAP ui5 application development tutorial - trial version of responsiveness of SAP ui5 applications
Uname command – displays system information
CST8227
Vscode voice notes to enrich information (medium)
Digitalization, transformation?
Tencent and China Mobile continued to buy back with large sums of money, and the leading Hong Kong stocks "led" the market to rebound?
Folding mobile phones are expected to explode, or help Samsung compete with apple and Chinese mobile phones
John
Processes and threads - concepts and process scheduling
CSDN cerebral palsy bug has wasted nearly two hours of hard work
Go quiz: considerations for function naming return value from the go interview question (more than 80% of people answered wrong)
SAP ui5 beginner tutorial 25 - using proxy server to solve the cross domain problem of SAP ui5 application accessing remote OData service trial version
Some common errors and solutions of using SAP ui5 to consume OData services
Notes on dashboard & kuboard installation in kubernetes cluster
Cat command – display the file contents on the terminal device
Websocket in the promotion of vegetable farmers
MySQL tuning --01--- optimization steps and system performance parameters