当前位置:网站首页>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()
}
边栏推荐
- Tencent and China Mobile continued to buy back with large sums of money, and the leading Hong Kong stocks "led" the market to rebound?
- How to open an account online? Is it safe to open an account online?
- Use of collection
- Mount command - file system mount
- Vegetables sklearn - xgboost (2)
- Go microservice development toolkit
- Cnpm installation
- [hand torn STL] Stack & queue
- What is SAP sup - Sybase unwired platform
- Pat 1045 quick sort
猜你喜欢
[golang] leetcode intermediate - Search rotation sort array & search two-dimensional matrix II
Folding mobile phones are expected to explode, or help Samsung compete with apple and Chinese mobile phones
SAP ui5 tutorial for beginners part XXVI - detailed steps for using OData service with mock server trial version
HashSet implementation class
Pre knowledge of asynchronous operation
Mongodb basic concept learning - Documentation
What happens when redis runs out of memory
Notes on dashboard & kuboard installation in kubernetes cluster
SAP Fiori tools and corresponding cli (command line interface)
Mongodb delete data
随机推荐
What happens when redis runs out of memory
Create a complete binary tree in array order
China rehabilitation hospital industry operation benefit analysis and operation situation investigation report 2022
Day21 JMeter usage basis
How SAP ui5 device type detection device API works
Highway
Copying DNA
Research Report on brand strategic management and marketing trends in the global and Chinese preserved fruit market 2022
Part 33 of SAP ui5 application development tutorial - trial version of responsiveness of SAP ui5 applications
An easy problem
Rational investment and internationalism
Distributed solar photovoltaic inverter monitoring
How the sap ui5 framework performs single step debugging of batch requests
Various errors and solutions encountered when deploying SAP ui5 application to ABAP server with SAP Fiori tools
Use of arrays tool class
Simple student management system
Global and Chinese benzoic acid market competition strategy and demand scale forecast report 2022
Leetcode sword finger offer question brushing - day 27
CSDN cerebral palsy bug has wasted nearly two hours of hard work
Go language map sorting (key/value sorting)