我有一个具有多个功能的类
class Foo() {
fun one() {
//do something
}
fun two() {
// do something
}
fun three() {
// do something
}
}
如何触发对我拥有的 Logger 对象的调用,以便在日志中我可以看到所有被访问或调用的函数,而无需显式地对每个函数进行日志调用以保持代码整洁。我试图保留服务 api 调用中调用的所有函数的完整日志跟踪,但我不希望有这样的东西
class Foo() {
fun one() {
log.call()
//do something
}
fun two() {
log.call()
// do something
}
fun three() {
log.call()
// do something
}
}
最佳答案
您无法在普通的 Kotlin 中做到这一点。但这正是那种事情Aspect-Oriented Programming用于。
我自己没用过,但如果你想在 Kotlin 中使用它,你可以看看 Spring AOP .另见讨论 here和 this问题。
关于kotlin - 如何在 Kotlin 中为类中的每个函数调用自动调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57067432/