色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

java中構造方法是否有返回值

謝彥文2年前115瀏覽0評論

java中構造方法是否有返回值?

按照官方文檔描述,java中構造方法是不具有返回值的。

下面是原文:

Providing Constructors for Your Classes

A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type. For example, has one constructor:

public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; }

To create a new object called , a constructor is called by the operator:

Bicycle myBike = new Bicycle(30, 0, 8);

creates space in memory for the object and initializes its fields.

Although only has one constructor, it could have others, including a no-argument constructor:

public Bicycle() { gear = 1; cadence = 10; speed = 0; }

invokes the no-argument constructor to create a new object called .

Both constructors could have been declared in because they have different argument lists. As with methods, the Java platform differentiates constructors on the basis of the number of arguments in the list and their types. You cannot write two constructors that have the same number and type of arguments for the same class, because the platform would not be able to tell them apart. Doing so causes a compile-time error.

You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of , which does have a no-argument constructor.

You can use a superclass constructor yourself. The class at the beginning of this lesson did just that. This will be discussed later, in the lesson on interfaces and inheritance.

You can use access modifiers in a constructor's declaration to control which other classes can call the constructor.

Note: If another class cannot call a constructor, it cannot directly create objects.

譯文如下:

為你的類提供構造方法

一個類包含多個構造方法,這些構造方法用于從類定義中創建對象。構造方法聲明很像方法聲明——除了它們使用的是類名作為方法名并且沒有返回類型。舉個例子,Bicycle 有一個構造方法:public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed;}為了創建名為myBike的一個新的Bicycle對象,構造方法被new操作符調用:Bicycle myBike = new Bicycle(30, 0, 8);new Bicycle(30, 0, 8)在內存中為對象創建了空間并且初始化了它的屬性。目前Bicycle只有一個構造方法,但是同時,它可以有更多構造方法,包括一個無參構造方法:public Bicycle() { gear = 1; cadence = 10; speed = 0;}...還有其他不做翻譯

Java官方文檔也描述有:對于構造方法來說,它與類方法類似,但是沒有返回類型,也意味著沒有返回值。

而為什么我們可以直接通過new來獲得一個返回值,那是因為new是一個操作符,new這個操作符調用了構造方法,并且返回了通過new操作符在內存中創建的空間地址。

結論:無返回值

java call,java中構造方法是否有返回值