如何避免儿童重复构造函数

编程入门 行业动态 更新时间:2024-10-20 16:32:50
本文介绍了如何避免儿童重复构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个父类,其构造函数为:

I have a parent class, which has constructor as:

@Inject public AbstractResource(@Named("authorization") Authorization auth, @Named("helper") Helper helper) { this.authorization = authorization; this.helper = helper; }

现在在children类中,我有类似的构造函数:

now in the children class, i have similar constructor:

public class MyResource extends AbstractResource { private Manager manager; @Inject public MyResource(@Named("authorization") Authorization auth, @Named("helper") Helper helper) { super(auth, helper); this.manager = new Manager(); } ...

问题是我有很多孩子上课了吗来自AbstractResource,我必须再次使用'Authorization'和'Helper'编写类似的构造函数。有什么办法可以避免重复编码?

Problem is I have tons of children class extend from AbstractResource, I have to write the similar constructor with 'Authorization' and 'Helper' again and agin. is there any way i can avoid the repetitive coding?

对不起,更新了我的代码,是的,我可以在每个子类中调用super(..),但是仍然在每个构造函数中我都注入了所有这些参数,auth和帮助者,只是想知道是否有一种方法可以简化该过程

Sorry, updated my code, yes, i can call super(..) in each children class, but still in each constructor i have inject all those parameters, auth and helper, just wonder if there is a way to even simplify that

推荐答案

除非我对此有所遗漏,否则有一个简单的方法

Unless I'm missing something about this there is a simple solution by calling through to the parent's constructor.

在子类的构造函数中,第一行应为: super(authorization,helper)

In your child class's constructor the first line should be: super(authorization, helper)

编辑:作者已编辑了问题,因此该解决方案不再适用。

Author has edited his question so this solution is no longer applicable.

更多推荐

如何避免儿童重复构造函数

本文发布于:2023-11-16 11:45:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603989.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   儿童

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!