一:单利
class FactoryClass{
//保存单例
static final FactoryClass _instance = FactoryClass._init();
//私有的构造函数
FactoryClass._init();
//工厂构造函数
factory FactoryClass()=>_instance;
}
二:如何实现类与类比较
可以用重载操作符实现
operatorTest(){
OperatorClass op1 = OperatorClass(20);
OperatorClass op2 = OperatorClass(18);
//正常情况下 是不可以比较对象的,需要重载操作符
print(op1 > op2);
}
class OperatorClass {
int age;
OperatorClass(this.age);
//重载操作符
bool operator > (OperatorClass other) => this.age > other.age;
}
网友评论