Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of visibility modifiers in Kotlin

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the use of visibility modifiers in Kotlin. It is very detailed and has a certain reference value. Friends who are interested must read it!

Preface

There are four visibility modifiers in Kotlin: private, protected, internal, and public. If the specified modifier is not displayed, the default visibility is public.

Description of four modifiers

The public modifier indicates public ownership. This modifier has the largest scope. This modifier is used by default when no modifier is declared.

The internal modifier represents the module. The scope of the module is described below.

The protected modifier represents a private `+` subclass. It is worth noting that this modifier cannot be used for top-level declarations, as you can see below.

The private modifier indicates private. This modifier has the smallest range, that is, the lowest visibility range.

1. The visibility modifier package com.iflytek.basekotlin.`class` / / functions, attributes, classes, objects and interfaces in the package scenario can all be declared at the top level, that is, 2021/6/22fun method () {} / / defaults to public directly in the package, and 2021/6/22private class ClassA () {} / / can only be seen in this file 2021/6/22privateval classA:ClassA = ClassA () / only use private here. Because ClassA is only visible in this file, its object is also 2021/6/22internal interface InterfaceA {} / / visible only in the same module 2021/6/22protected var name:String = "" / / error: Modifier 'protected' is not applicable to' top level property without backing field or delegate' 2021-6-22

If you do not specify any visibility modifiers, the default is public, which means it can be seen everywhere

If declared as private, it is only visible in the declared file

If declared as internal, it is only visible within the same module

Protected does not apply to top-level declarations, that is, to this scenario

2 、 Open class VisibilityDemo {privateval a = true / / members declared inside the class are only visible within the class 2021-6-22 protected val b = true / / within the class and in its subclasses 2021-6-22 internal val c = true / / any client of this module that can see the class declaration is visible 2021-6-22 val d = true / / any client that can see the class declaration is visible 2021 / 6 take 22} class SubVisibility:VisibilityDemo () {fun visible () {/ / println (a) / / not visible 2021-6-22 println (b) / / visible 2021-6-22 println (c) / / visible 2021-6-22 println (d) / / visible 2021-6-22}} class UnRelated () {fun visible (visibilityDemo: VisibilityDemo) {/ / Println (visibilityDemo.a) / / Invisible 2021 visibilityDemo.b 6x22 println / / Invisible 2021-6-22 println (visibilityDemo.c) / / visible 2021-6-22 println (visibilityDemo.d) / / visible 2021-6-22}}

Private is visible only within the class

Protected is visible in the class and its subclasses

Internal can see the class declaration visible in this module

Public can be seen at the class declaration.

The above is all the content of the article "what is the use of visibility modifiers in Kotlin". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report