私有变量/属性和方法如何工作? [英] How private variable/property and method works?

查看:56
本文介绍了私有变量/属性和方法如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我去过文档和教程,我知道我们需要使用underscore来定义属性或方法,但是通过下面的代码,我仍然可以访问它.

As far I been to doc's and tutorials I got to know we need to use underscore to define properties or methods but from by below code I can still access it.

void main() {
  User d = new User('John Doe', 5);
  print(d._name);
}

class User {
  String _name;
  int age;

  User(this._name, this.age);

  String respectedName(nname) {
    return 'Mr.$nname';
  }
}

我不确定我是否了解Dart的工作原理,请提供帮助.

I am not sure that I understand how things works in Dart, please help.

此处,有一个微笑的问题得到了回答,但这还不是很多对我有帮助的答案.

There is smiler question answered here, but that's still not much of a helpful answer to me.

推荐答案

dart中的私有属性/方法可用于整个文件,而不能从其他文件访问.

Private properties/methods in dart are accessible for the whole file and not accessible from other files.

这可以与part/part of关键字结合使用,将两个以上的文件视为一个文件.

This can be combined with part/part of keyword to treat two+ files as one.

这篇关于私有变量/属性和方法如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆