英文:
duplicate method in javap result
问题
在BaseController中有一个方法,例如
public abstract class BaseManagementController<V, F extends BaseForm> {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
protected void add(@Valid @RequestBody F form, HttpServletRequest request) {
// ...
}
}
还有一个具体的Controller继承它并覆盖add方法
public class BannerController extends BaseManagementController<BannerVO, BannerForm> {
@Override
public void add(@Valid @RequestBody BannerForm form, HttpServletRequest request) {
super.add(form, request);
}
}
当我使用javap
命令查看BannerController时,我看到了两个add方法
public void add(com.foo.admin.web.vo.BannerForm, javax.servlet.http.HttpServletRequest);
descriptor: (Lcom/foo/admin/web/vo/BannerForm;Ljavax/servlet/http/HttpServletRequest;)V
flags: ACC_PUBLIC
public void add(com.foo.admin.web.vo.BaseForm, javax.servlet.http.HttpServletRequest);
descriptor: (Lcom/foo/admin/web/vo/BaseForm;Ljavax/servlet/http/HttpServletRequest;)V
flags: ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC
为什么会有两个add方法?这是方法重载吗?
英文:
There is a method in BaseController, e.g.
public abstract class BaseManagementController<V, F extends BaseForm> {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
protected void add(@Valid @RequestBody F form, HttpServletRequest request) {
// ...
}
}
and a concrete Controller extends it and override add method
public class BannerController extends BaseManagementController<BannerVO, BannerForm> {
@Override
public void add(@Valid @RequestBody BannerForm form, HttpServletRequest request) {
super.add(form, request);
}
}
when I javap
BannerController I see two add method
public void add(com.foo.admin.web.vo.BannerForm, javax.servlet.http.HttpServletRequest);
descriptor: (Lcom/foo/admin/web/vo/BannerForm;Ljavax/servlet/http/HttpServletRequest;)V
flags: ACC_PUBLIC
public void add(com.foo.admin.web.vo.BaseForm, javax.servlet.http.HttpServletRequest);
descriptor: (Lcom/foo/admin/web/vo/BaseForm;Ljavax/servlet/http/HttpServletRequest;)V
flags: ACC_PUBLIC, ACC_BRIDGE, ACC_SYNTHETIC
why have two add method? Is it overload?
答案1
得分: 0
因为可能存在一些功能对于所有Controllers
都更具有普遍适用性,并且这种通用功能可以在*BaseManagementController.add()
*中实现...
public abstract class BaseManagementController<V, F extends BaseForm> {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
protected void add(@Valid @RequestBody F form, HttpServletRequest request) {
/* ...所有控制器都需要做这个... */
controlStuff(form, request);
}
private void controlStuff(F form, HttpServletRequest request) {
/* ...做控制器通常要做的事情... */
}
}
然后你的更专业化的实现,比如_BannerController.add()
_,可以只处理与横幅相关的事务...
public class BannerController extends BaseManagementController<BannerVO, BannerForm> {
@Override
public void add(@Valid @RequestBody BannerForm form, HttpServletRequest request) {
super.add(form, request);
processBanner(form, request);
}
private void processBanner(BannerForm form, HttpServletRequest request) {
/*...为横幅执行特殊操作...*/
}
}
子类的_add()
方法可以简单地调用super.add()
_方法,并在其父类的实现中利用现有功能。
不是。但它是重写...
> …
>
> 如果在类C中声明或继承的实例方法<code>m<sub>C</sub></code>重写了类A中声明的另一个方法<code>m<sub>A</sub></code>,则当且仅当满足以下所有条件时,C从A中重写了<code>m<sub>A</sub></code>:
>
> - C是A的子类。
> - C不继承<code>m<sub>A</sub></code>。
> - *m<sub>C</sub>的签名是m<sub>A</sub>的子签名(§8.4.2) *。
> - 以下情况之一成立:
> - <code>m<sub>A</sub></code>是公共的。
> - ...
英文:
> „why have two add method?“
Because there could be some functionality that might be more generally-applicable to all Controllers
. And that general functionality could be implemented in BaseManagementController.add()
…
public abstract class BaseManagementController<V, F extends BaseForm> {
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
protected void add(@Valid @RequestBody F form, HttpServletRequest request) {
/* ...All Controllers need to do this... */
controlStuff(form, request);
}
private void controlStuff(F form, HttpServletRequest request) {
/* ...do whatever Controllers do...*/
}
}
Then your more specialized implementations like BannerController.add()
could do something banner-related only…
public class BannerController extends BaseManagementController<BannerVO, BannerForm> {
@Override
public void add(@Valid @RequestBody BannerForm form, HttpServletRequest request) {
super.add(form, request);
processBanner(form, request);
}
private void processBanner(BannerForm form, HttpServletRequest request) {
/*...do something special for banners...*/
}
}
The subclass's add()
could simply call super.add()
and piggyback off of existing functionality in its super's implementation.
> „Is it overload?“
No. But it is overridden…
> …
>
> An instance method <code>m<sub>C</sub></code> declared in or inherited by class C overrides from C another method <code>m<sub>A</sub></code> declared in class A, iff all of the following are true:
>
>
> - C is a subclass of A.
> - C does not inherit <code>m<sub>A</sub></code>.
> - The signature of m<sub>C</sub> is a
subsignature (§8.4.2) of the signature of <code>m<sub>A</sub></code>.
> - One of the following is
true:
> - <code>m<sub>A</sub></code> is public.
> - ...
专注分享java语言的经验与见解,让所有开发者获益!
评论