我有两节课
class A {
String _id;
static final Map<String, A> _instances = <String, A>{};
factory A.getInstance(String id){
if(!_instances.containsKey(id)){
_instances[id] = new A._internal(id);
}
return _instances[id];
}
A._internal(this._id);
}
class B extends A {
factory B.getInstance(int id){
return new A.getInstance(id.toString());
}
}
我知道这不是一个好习惯,因为 A 类不是 B 类的子类型。有没有办法做到这一点?