强制调用父方法 [英] Enforcing call to parent method
问题描述
我有一个像这样的抽象类:
抽象类APrimitive {
public function validate(){
//做一些应用所有扩展APrimitive $ b $的类的东西b}
}
然后我有类扩展到APrimitivebase:
class CSophisticated扩展APrimitive {
public function validate(){
//首先调用父版本:
parent :: validate();
//然后在这里做更复杂的事情。
}
}
问题是如果我们回到代码在几个月的时间里,再创建一些类似于CS复杂的类,一个 validate()
方法,我们可能会忘记拨打 parent :: validate()
。
请注意,一些CS复杂类可能没有验证()
方法,所以父版本将被调用。
我知道有可能在某处发表评论,提醒程序员调用 parent :: validate()
,但是有更好的方法吗?如果调用 parent :: validate()
没有在 validate()
方法将会很好。
您可以使用以下方式强制执行该电话:
抽象类APrimitive {
final public function validate(){
//执行逻辑验证
overrideValidate();
}
protected function overrideValidate(){
}
}
class CS复制扩展APrimitive {
protected function overrideValidate ){
}
}
现在只调用<允许$ c $ c>验证,这将反过来称为您重写的方法。语法可能有点偏离( PHP
不是我的选择语言),但原则可以应用于大多数OOP语言。
进一步说明:
抽象类APrimitive {
public function validate(){
echo 'APrimitive validate call'。
overrideValidate();
}
protected function overrideValidate(){
}
}
class CS复制扩展APrimitive {
protected function overrideValidate ){
echo'CS复合调用'。
}
}
复杂的foo;
foo.overrideValidate(); // error - overrideValidate被保护
foo.validate(); //
输出:
APrimitive验证通话。
CS复合电话。
函数调用基本上执行以下操作:
foo.validate() - > APrimitive.validate() - > ASophisticated.overrideValidate()(或APrimitive.overrideValidate()如果没有被覆盖)
Is there anyway (or a pattern) to enforce a call to a parent method?
I have an abstract class like so:
abstract class APrimitive{
public function validate(){
//Do some stuff that applies all classes that extend APrimitive
}
}
Then I have classes that extend upon the APrimitive "base":
class CSophisticated extends APrimitive{
public function validate(){
//First call the parent version:
parent::validate();
//Then do something more sophisticated here.
}
}
The problem is that if we come back to the code in a few months time, and create a few more classes like CSophisticated with a validate()
method, there is a possibility that we might forget to make a call to parent::validate()
in that method.
Note that some CSophisticated classes may not have the validate()
method, so the parent version will be called.
I understand that it is possible to just put in a comment somewhere, to remind the programmer to call parent::validate()
, but is there a better way? Perhaps an automated way to throw an exception if the call to parent::validate()
was not made in the validate()
method would be nice.
You can enforce the call with the following:
abstract class APrimitive{
final public function validate(){
//do the logic in validate
overrideValidate();
}
protected function overrideValidate(){
}
}
class CSophisticated extends APrimitive{
protected function overrideValidate(){
}
}
Now only calls to validate
are permitted, which will in turn call your overridden method. The syntax may be a little off (PHP
is not my language of choice) but the principle is applyable to most OOP languages.
FURTHER EXPLANATION:
abstract class APrimitive{
public function validate(){
echo 'APrimitive validate call.';
overrideValidate();
}
protected function overrideValidate(){
}
}
class CSophisticated extends APrimitive{
protected function overrideValidate(){
echo 'CSophisticated call.';
}
}
CSophisticated foo;
foo.overrideValidate(); //error - overrideValidate is protected
foo.validate(); //
Output:
APrimitive validate call.
CSophisticated call.
The function call basically does the following:
foo.validate() -> APrimitive.validate() -> ASophisticated.overrideValidate() (or APrimitive.overrideValidate() if it wasn't overriden)
这篇关于强制调用父方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!