How to create private variables in Dart?

后端 未结 4 473
夕颜
夕颜 2020-12-03 00:20

I want to create a private variable but I cannot.

Here is my code:

void main() {
  var b = new B();
  b.test         


        
4条回答
  •  生来不讨喜
    2020-12-03 01:07

    From Dart documentation:

    Unlike Java, Dart doesn’t have the keywords public, protected, and private. If an identifier starts with an underscore _, it’s private to its library.

    Libraries not only provide APIs, but are a unit of privacy: identifiers that start with an underscore _ are visible only inside the library.

    A few words about libraries:

    Every Dart app is a library, even if it doesn’t use a library directive. The import and library directives can help you create a modular and shareable code base.

    You may have heard of the part directive, which allows you to split a library into multiple Dart files.

提交回复
热议问题