简体   繁体   中英

Why don't I need to write property at object class in sealed class by Kotlin?

I'm developing the app by using Kotlin.

sealed class DestinationScreen(val route:String){
    object Signup: DestinationScreen(route = "signup")
}

@Composable
fun InstagramApp(){
 
    val navController = rememberNavController()
    
    NavHost(navController = navController, startDestination = DestinationScreen.Signup.route){
        composable(DestinationScreen.Signup.route){
            SignupScreen(navController = navController)
        }
    }
}

I don't know Why Signup singleton class can have the property "route" using argv? I understand it inherits DestinationScreen. So it also has route property.

But Destination class doesn't have concrete the property route . If Destination class is data class, make sense it doesn't need to declare the property. No need for {} . And data class has the property not declareing it by using argv. So I mean DestinationScreen should has concrete property route , if Signup inherit different property's value, it should override. Why can this codes above work? Does this feature have seal class or object class?

Please teach me. Thank you.

But Destination class doesn't have concrete the property route

Yes, it does. The route property is declared right there in its constructor.

if Signup inherit different property's value, it should override

Not sure what you mean by this, but Signup doesn't need to override the property. It already inherits the property. By passing a value to the super-class's constructor, the existing property gets an initial value as passed by the sub-class without overriding it.

You mention sealed and data class types, but they are irrelevant to this discussion. Inheritance works the same way with sealed and non-sealed classes.

Any time a class extends another class, it also is a type of that class and inherits all of its properties and functions, no overriding needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM