当覆盖方法应我的自定义code来之前或之后超(基地) [英] When overriding a method should my custom code come before or after super(base)

查看:162
本文介绍了当覆盖方法应我的自定义code来之前或之后超(基地)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当覆盖方法应我的自定义code来之前或超(基地)调用父类后?

When overriding a method should my custom code come before or after the super(base) call to the parent class?

推荐答案

有3个选择,你在这里:

There are 3 choices you have here:

  • 如果你想在你的code 执行基地的行为,那么之前调用它。
  • 如果您想在您code执行基础行为,然后后调用它。
  • 如果你想为完全覆盖基本行为,别叫它。
  • If you want to execute the base behavior before your code, then call it before.
  • If you want to execute the base behavior after your code, then call it after.
  • If you want to completely override the base behavior, don't call it at all.

重要的是要还的检查您的API文档。有些类有子类合同中都没有enforcable由code,但可以打破的行为,如果你不遵守他们的规则。也有一些情况下,子类都必须调用的超实施

It is important to also check your API's documentation. Some classes have subclass contracts that are not enforcable by code, but that can break behavior if you don't follow their rules. There are some cases where subclasses are required to call the super implementation.

这篇关于当覆盖方法应我的自定义code来之前或之后超(基地)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆