我真的需要在java中定义默认构造函数吗? [英] Do I really need to define default constructor in java?

查看:264
本文介绍了我真的需要在java中定义默认构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它没有定义构造函数时工作正常,但如果我定义一个参数化构造函数而不是默认的一个并且在创建对象时不传递任何值,则会产生错误。我认为构造函数是预定义的。

It works fine when constructors are not defined, but gives errors if I define a parameterized constructor and not a default one and not passing any values while creating an object. I thought constructors are predefined.

为什么我需要定义一个默认构造函数,如果我已经定义了参数化构造函数?是不是默认构造函数是预定义的?

Why do I need to define a default constructor if I've defined a parameterized constructor? Ain't default constructor predefined?

推荐答案

仅自动创建默认(无参数)构造函数

A default (no-argument) constructor is automatically created only when you do not define any constructor yourself.

如果你需要两个构造函数,一个有参数,一个没有,你需要手动定义两个。

If you need two constructors, one with arguments and one without, you need to manually define both.

这篇关于我真的需要在java中定义默认构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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