Java中的默认构造函数(Default constructors in Java)

系统教程 行业动态 更新时间:2024-06-14 17:03:54
Java中的默认构造函数(Default constructors in Java)

我知道我在这里问一些严重的101个问题

我有一些类Foo和一个扩展Foo的类Bar 。 在Foo我有一个构造函数,它将一组参数设置为其字段。 派生类(例如Bar通常不需要修改这个。 现在我的IDE给了我“Foo中没有默认的构造函数” 。 来自谷歌的一些这似乎是因为“构造函数不被继承”。 所以很好,很好,但是如何在每个派生类中重复这个构造函数呢? 我假设有一个更健全的方法?

I know I'm asking some serious 101 question here...

I have some class Foo and a class Bar that extends Foo. In Foo I have a constructor that takes a set of parameters that it sets to its fields. Deriving classes such as Bar will typically not need to modify this. Now my IDE is giving me "There is no default constructor available in Foo". From a bit of Googling this appears to be because "constructors are not inherited". So all nice and well, but how do I now get this to work without duplicating this constructor in every deriving class? I'm assuming there is a more sane approach?

最满意答案

使用super构造函数:

public Bar(int a, double b, ...) { super(a, b, ...); }

Use the super constructor:

public Bar(int a, double b, ...) { super(a, b, ...); }

更多推荐

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

发布评论

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

>www.elefans.com

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