gpt4 book ai didi

java - 构造函数和传递参数有什么区别?

转载 作者:行者123 更新时间:2023-11-30 04:13:20 27 4
gpt4 key购买 nike

我相信我现在了解构造函数是什么,但对我来说,它们似乎只是用于将信息传递给另一个类中的方法。那么为什么不直接调用一个方法并提供一个参数呢?我有错吗?

最佳答案

构造函数是用来创建对象的,不是普通的方法。每当您使用new创建对象时,您实际上调用了构造函数。例如:

new MyClass(); //here MyClass() is a constructor with no params

Note :

Constructor declarations look like method declarations—except that they use the name of the class and have no return type

关于java - 构造函数和传递参数有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19069195/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com