Kotlin中调用父类内部类(ImageSwitcher.LayoutParams)失效求助
ImageSwitcher.LayoutParams Compiles in Java but Fails in Kotlin? Great question! This difference comes down to how Java and Kotlin handle inheritance of nested classes—here's a breakdown of the key rules:
Java's implicit nested class access
In Java, when a subclass (likeImageSwitcher) extends a parent class (likeFrameLayout), non-private nested classes of the parent (such asFrameLayout.LayoutParams) are implicitly "inherited" in a syntactic sense. The compiler lets you reference the parent's nested class using the subclass's name as a shortcut—soImageSwitcher.LayoutParamsis just sugar that resolves toFrameLayout.LayoutParamsunder the hood. Java does this to make code feel more intuitive when working with subclass APIs.Kotlin's strict nested class resolution
Kotlin takes a more explicit approach to nested class access. It does not automatically expose a parent's nested class as part of the subclass's namespace. SinceImageSwitcherdoesn't define its ownLayoutParamsinner class, writingImageSwitcher.LayoutParamsthrows a compilation error—Kotlin won't assume you're referring to the parent class's nested type. You have to explicitly useFrameLayout.LayoutParamsto reference the correct class.Kotlin's design choice: avoiding ambiguity
This strictness is intentional. IfImageSwitcherever defined its ownLayoutParamsinner class later,ImageSwitcher.LayoutParamswould clearly point to the subclass's version. Java's implicit behavior, by contrast, could lead to confusing or unexpected references if a subclass adds a nested class with the same name later. Kotlin eliminates that ambiguity by requiring explicit qualification.
In short: Java allows lenient, implicit access to a parent's nested classes via a subclass, while Kotlin enforces explicit, unambiguous references to avoid confusion.
内容的提问来源于stack exchange,提问作者humazed

