猿问

为什么 Car 类会存储对象 car 和 car2 的正确信息,而 Owner 类不会存储对象

我是 Java 新手。我很抱歉这么长。直接下面是我的代码到目前为止的输出带有**的部分是我遇到问题的地方。我应该比较两个 Car 对象的速度,但每个 Owner 对象要么是“安全”的,要么是“鲁莽的”驱动程序。如果他们鲁莽,当他们加速时,他们会加速 10。如果他们是安全的,他们会加速 5。到目前为止,这方面是有效的,但是,只有第一个 Owner 对象会存储信息。因此,无论第一个 Owner 对象是什么(Safe/rash),它们都会是。这不会发生在我的 Car 对象上,因为每个单独的对象都存储了正确的信息,而这正是我遇到困难的地方。谁能帮我弄清楚为什么?


Car Name: Betty

Year: 2002

Car Make: Kia

Car Model: Soul

Car Mileage: 50000

Car Speed: 65

Car owner name: Jane

Car owner type: rash

Car Name: Duke

Year: 2002

Car Make: Ford

Car Model: F250

Car Mileage: 50000

Car Speed: 65

Car owner name: John

Car owner type: safe



Betty is a 2002 Kia Soul.

The current mileage is 50000 and current speed is 65.

Jane is the owner of the car and is a rash driver.


Duke is a 2002 Ford F250.

The current mileage is 50000 and current speed is 65.

**Jane is the owner of the car and is a rash driver.**


The total mileage of both cars is 100000

Betty is going 75.

**Duke is going 75.**

我是 Java 新手。我很抱歉这么长。直接下面是我的代码到目前为止的输出带有**的部分是我遇到问题的地方。我应该比较两个 Car 对象的速度,但每个 Owner 对象要么是“安全”的,要么是“鲁莽的”驱动程序。如果他们鲁莽,当他们加速时,他们会加速 10。如果他们是安全的,他们会加速 5。到目前为止,这方面是有效的,但是,只有第一个 Owner 对象会存储信息。因此,无论第一个 Owner 对象是什么(Safe/rash),它们都会是。这不会发生在我的 Car 对象上,因为每个单独的对象都存储了正确的信息,而这正是我遇到困难的地方。谁能帮我弄清楚为什么?


Car Name: Betty

Year: 2002

Car Make: Kia

Car Model: Soul

Car Mileage: 50000

Car Speed: 65

Car owner name: Jane

Car owner type: rash

Car Name: Duke

Year: 2002

Car Make: Ford

Car Model: F250

Car Mileage: 50000

Car Speed: 65

Car owner name: John

Car owner type: safe



Betty is a 2002 Kia Soul.

The current mileage is 50000 and current speed is 65.

Jane is the owner of the car and is a rash driver.


Duke is a 2002 Ford F250.

The current mileage is 50000 and current speed is 65.

**Jane is the owner of the car and is a rash driver.**


The total mileage of both cars is 100000

Betty is going 75.

**Duke is going 75.**


慕慕森
浏览 134回答 1
1回答

Helenr

双方car并car2得到相同的owner构造函数实例。其中之一应该是owner2。car2 = new Car(carName, yearModel, make, model, mileage, speed, owner2);                                          // here ^
随时随地看视频慕课网APP

相关分类

Java
我要回答