构造函数有不同类型
- 默认构造函数
默认构造函数也被称为没有参数构造函数,如果我们未声明默认构造函数,则会自动创建它。
DART默认构造函数的示例
class DefaulConstruct{
DefaulConstruct() {
print("I am a Default Constructor");}
}
void main() {
DefaulConstruct obj = new DefaulConstruct();
}
- 命名为构造函数
我们可以使用命名的构造函数来为类实现多个构造函数。 - 工厂建设者
使用工厂构造函数,当我们想要仅创建一次;并使用此实例多次。 - 常量构造函数
不可变编译时间常量对象称为const对象;常量构造函数为它而设计。
常量构造函数的示例
class constcon{
final int a;
const constcon(this.a);
}
main(){
var a = const constcon(1);
var b= const constcon(1);
print("The value of with const constructor is ${a==b}");
/And if you check without const **/
var c = new constcon(1);
var d = new constcon(2);
print("The value of WithOut const constructor is ${c==d}");
}
Dart中的构造函数
构造函数是具有相同名称类的DART语言中的类成员函数。
构造函数不会被子类继承。
构造函数的示例
class Construct{
Construct.MyConstruct(){
print("Wecome in Dart Constructor !");
}
Construct.MyConstruct1(String s){
print("Wecome in Parametrized Constructor !");
}
}
void main()
{
Construct obj = new Construct.MyConstruct();
Construct obj1 = new Construct.MyConstruct1("Dart");
}
日期:2020-04-11 23:03:58 来源:oir作者:oir
