gpt4 book ai didi

java - 直观理解

转载 作者:行者123 更新时间:2023-11-29 10:11:48 24 4
gpt4 key购买 nike

在此页面上:https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

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, Bicycle has one constructor:

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

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

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

new Bicycle(30, 0, 8) creates space in memory for the object and initializes its fields."

所以我之前和那里都做过 JS,声明一个新对象类似于 var newObject = new Bicycle(params);

但是,我在这里注意到,当您创建一个新对象时,您说 Bicycle myBike = new Bicycle(params);

为什么自行车这个词被用了两次?它是如何工作的?

最佳答案

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

在一条语句中发生了两件主要的事情:

1.

Bicycle myBike;

这是变量声明,其中指定了变量名称和类型。

2.

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

这是变量初始化,赋值的地方。

关于java - 直观理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31251940/

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