Kotlin programming Visibility modifier (private,protected,internal,public)

Source: Internet
Author: User
Tags constructor modifier modifiers visibility

Kotlin Programming related knowledge points : Kotlin programming using IntelliJ IED and understanding Source files (. kt) Kotlin programming and other properties Kotlin programming methods such as using Kotlin programming Kotlin programming of the parent class and the inherited parent class Kotlin programming interface and implementation interface Kotlin programming of the associated object, abstract class, sealed class Kotlin programming nested class, Inner class, Anonymous inner class Kotlin programming object expression and declaration Kotlin programming extension method Kotlin programming of extended attributes and extended companion objects Kotlin programming of generic Kotlin programming if statement, while loop, when expression, for loop

There are four modifiers in Kotlin programming: Private,protected,internal,public, the default modifier is public. These modifiers can be used in the set () of classes, objects, interfaces, constructors, properties, and properties. Package-level usage modifiers :

Package com.xingen.kotlin.day2017529.pm

Private  test1 () {} public

var v= "anywhere you see"

private var  v2= "only visible in the source file"

internal  val v3= "visible under the same module"

Note: Protected can not be used in "top-level", that is, the package-level methods or properties cannot be modified, etc.

The private declaration is visible in the source file that contains the declaration

Internal declaration, anywhere in the same module is visible in the class and the interface uses modifiers

Package com.xingen.kotlin.day2017529.pm Fun

Main (args:array<string>) {
    test (). Test ()
}
Open  class basetest{
    private Val a= "This property cannot be accessed by the Quilt class
    protected var b=" This property can be accessed by the Quilt class
    internal var c= "under the same module"
    var d= "can be accessed everywhere"
}
Class Test:basetest () {
    fun  Test () {
        println (b)
        println (c)
        println (d)
    }
}

Output Result:

This property can be accessed
everywhere under the same module that the Quilt class accesses

Know:

Private is visible only in the class (and its members) +

Protected is the same as private, but it is also visible in subclasses internal all internal members of the class that can access the declaration area in this module can access the modifier in the visible constructor anywhere in the public

Class C Private Constructor (A:int) {...}

Specifies that the primary constructor is not accessible, and the privatization Local declaration

Local variables, functions, and classes are modifiers that are not allowed in a module that uses modifiers

In Kotlin programming, a module refers to a project that contains the following items: An IDE-created module a MAVEN or Gradle project compiles a set of files by invoking the ant task one at a time

The internal modifier means that the visibility of a member is visible only in the same module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.