zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java的构造器和void方法的差别

JAVA方法 构造 差别 void
2023-09-27 14:25:17 时间

void型的方法什么都不返回,但这是你决定的,你也能够让它返回些什么。而构造函数则什么都不返回,并且你别无选择.”----这句话的意思是。是你决定让这种方法返回void类型的。你也能够改变它不让它返回的是void,而返回其它的类型如int,String等。可是对构造方法你就没机会决定它的返回类型是什么了。

The constructor is an unusual type of method because it has no return value. This is distinctly different from a void return value, in which the method returns nothing but you still have the option to make it return something else. Constructors return nothing and you don’t have an option.

构造器时在堆中开辟内存空间。创建对象的过程。

假设某方法使用构造器创建对象,返回的是对象地址。

用參数接受此对象地址。这个是约定俗成的规则不能写返回值。

如 Student s = new Student();

普通方法无论返回void或者string,对象,都是能够选择的。而构造器绝对不能。