作为软件开发人员,我想为我的客户提供扩展库。不应更改库提供程序的原始库。
有几种方法可以做到这一点。我想到了特质,但也有遗传。
假设原始库中有一个类定义为:
procedure TfrmPatient.btnRemovePatientClick(Sender: TObject);
var
iPatientID : Integer;
begin
iPatientID := 0;
iPatientID := StrToInt(InputBox('Delete Patient', 'Enter Patient ID to Delete',''));
DBPatients.Close;
DBPatients.SQL.Text := 'Delete from tblPatients Where PatientID = ' + IntToStr(iPatientID);
DBPatients.ExecSQL;
end;
第一种方法:使用特征扩展原始库:
class Super {}
第二种方法:使用继承扩展原始库:
trait MyTrait {
public function func() {
echo "func in MyTrait\n";
}
}
// Customer writes in his code:
class Sub1 extends Super {
use MyTrait;
}
$sub1 = new Sub1;
$sub1->func();
在这种情况下使用traits与inheritace有什么好处? 在哪种情况下哪种方法更有限?哪一个为我作为软件开发人员或客户提供了更大的灵活性?
如果我们处于开源或闭源域,这些方法有什么不同吗?
这种情况有更好的方法吗?
答案 0 :(得分:0)
很难推荐一种方法而不是另一种方法,但在许多情况下,组合是更适合为最终用户提供灵活性的方法。
考虑你的特质样本:
RuntimeError: maximum recursion depth exceeded while calling a Python object
可以这样改写:
self.house = House()