0
Follow
0
View

How does Java call a constructor with arguments

cxbnet1 注册会员
2023-02-26 16:13

  • In Java, if a class defines a parameterized constructor, you can use that constructor to create an object and pass the constructor arguments to it.
  • However, in your code, it is incorrect to try to call the parameterized constructor as model2.Model("GG"," male ",22,20.0,70.0,50.0). This is because the constructor is not a normal method and cannot be called directly. Instead, when you create an object, Java automatically calls the appropriate constructor to initialize the object.
  • Therefore, to call a constructor with arguments, you need to use the new operator to create the object and pass the arguments to the constructor. For example, the first-floor method creates a Model object with the new operator and passes the parameters "GG", "male", 22, 20.0, 70.0, and 50.0 to the object's parametered constructor. In this way, Java automatically calls the constructor to initialize the object and then assigns it to the model2 variable.

asdw7540522 注册会员
2023-02-26 16:13

The following answers are quoted from GPT-3 model, please use them wisely:

无参构造方法Model(),你的代码是在调用无参构造方法new Model()之后,又去调用Model("GG","男",22,20.0,70.0,50.0)这个方法,但是这并不是一个构造方法,而只是一个普通的带参数的方法,所以无法完成构造实例对象。

如果要调用有参构造方法,需要指定参数,格式如下:
Model model2 = new Model("GG","男",22,20.0,70.0,50.0);

Please accept my answer if it answers your question

deathxc 注册会员
2023-02-26 16:13

The java constructor is used to construct this object. The constructor cannot be called through the instantiated object.

Model model = new Model("GG","男",22,20.0,70.0,50.0); 

About the Author

Question Info

Publish Time
2023-02-26 16:13
Update Time
2023-02-26 16:13

Related Question

Authorizing Swagger calls when using Istio Authorization

Java在物联网方面有什么应用?

如何使用RSA_PKCS1_OAEP_PADDING加密在Java

在Javascript中获取setTimeout内函数的返回项

Gradle检测Java 1.8而不是Java 11

java中synchronized可见性问题

在Oracle apex中使用动态动作javascript提交页面

在环境变量中增加javac命令路径

javascript的数组怎么去重

Java怎么读取word文档的自动编号