_internal的语义是什么? [英] What are the semantics of _internal?
问题描述
在以下示例中,我不断看到对_internal的引用:
I keep seeing references to _internal in examples like the following:
class Symbol {
final String name;
static Map<String, Symbol> _cache;
factory Symbol(String name) {
if (_cache == null) {
_cache = {};
}
if (_cache.containsKey(name)) {
return _cache[name];
} else {
final symbol = new Symbol._internal(name);
_cache[name] = symbol;
return symbol;
}
}
Symbol._internal(this.name);
}
我从代码中收集到它是一个私有的构造函数。最后一行 Symbol._internal(this.name);
似乎有点令人困惑,因为它似乎是类主体中的语句,而不是方法主体中的语句,这导致我相信这实际上是没有方法主体的内部构造函数定义。
I've gathered from the code that it's a privately accessible constructor. The last line Symbol._internal(this.name);
seems a bit confusing because it appears to be a statement within the class body and not within a method body, leading me to believe it's actually the internal constructor definition without a method body.
我的假设正确吗?
推荐答案
_internal 构造只是一个经常为该类私有的构造函数的名称(不需要为 ._ internal 。使用任何 Class._someName 结构创建私有构造函数。)
The _internal construction is just a name often given to constructors that are private to the class (the name is not required to be ._internal you can create a private constructor using any Class._someName construction).
例如,以下代码仅允许您使用缓存构造函数从类外部创建新人员。 :
For example the following code only allows you to create new persons from outside the class using a caching constructor:
class Person {
final String name;
static Map<String,Person> _cache;
factory Person(String name) {
if(_cache === null) {
_cache = new Map<String,Person>();
}
if(_cache[name] === null]) {
_cache[name] = new Person._internal(name);
}
return _cache[name];
}
Person._internal(this.name);
}
通常,Dart会将任何 _construction 视为班级或班级的私有对象包含它的库。例如,您可以定义为这样的全局函数:
In general Dart treats any _construction as private to either the class or the library that contains it. For example you can define as a global function like this:
_globalToThisLibaryOnly() {
print("can only be called globally within this #library");
}
可以从定义它的库中获取的任何文件中调用。
which can be called from any file that is sourced within the library that defines it.
这篇关于_internal的语义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!