Java方法与构造函数参数比较

huangapple go评论52阅读模式
英文:

Java Methods vs constructors parameter comparison

问题

实际上,我是初学者,我的问题可能很傻。我的疑问是 Java 中方法(methods)和构造方法(constructors)的实际目的是什么,

我们可以在方法和构造方法的参数中传递值,

哪种方式被推荐?为什么?

英文:

actually i'm beginner my questions might be silly And my doubt is what was actual purpose of methods vs constructors in java,

we can pass values in both methods & constructor parameters,

which one is recommended and why??

答案1

得分: 0

构造函数用于初始化对象,而方法用于展示对象的功能。

构造函数隐式调用,而方法需要显式调用。

构造函数不返回任何值,而方法可能返回或不返回值。

如果没有构造函数,Java编译器会提供默认构造函数。但对于方法,不会提供默认方法。

构造函数的名称应与类名相同。方法名称不应与类名相同。

英文:

Constructor is used to initialize an object whereas method is used to exhibits functionality of an object.

Constructors are invoked implicitly whereas methods are invoked explicitly.

Constructor does not return any value where the method may/may not return a value.

In case constructor is not present, a default constructor is provided by java compiler. In the case of a method, no default method is provided.

Constructor should be of the same name as that of class. Method name should not be of the same name as that of class.

答案2

得分: 0

一个Java方法用于执行某些操作,也称为函数(Function)。在其中,您可以传递参数。它们必须有返回类型。

构造方法是一种特殊的方法,用于初始化对象。构造方法不能有返回类型。构造方法的名称必须与类名相同。

英文:

A Java method is use to perform some action which is also know as Function. Where you can pass parameter. They must have return type.

A Constructor is special method use to initialise the object . Constructor must not have return type. Constuctor name must be same as class name.

huangapple
  • 本文由 发表于 2020年8月27日 12:11:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63609058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定