这是一个实践的理论问题。
问题是
Create an immutable class
Car
.Create some instances of car to fill an
Arraylist<Car>
inside aGarage
class.The
MyGarage
class implements these methods fromGarage
:
getCar(String reg)
– search for the car with registration numberreg
.getMake(String make)
– returns a list of cars that match the givenmake
.totalValue()
– calculates the total value of all cars in the list.changeOwner(String reg, String ow)
– change the owner of car that has registration numberreg
toow
.
我不明白changeOwner
方法,因为它不应该能够更改我认为不可变类的实例???
这就是我为解决这个问题所做的事情,但看起来很愚蠢
import java.util.ArrayList;
public class MyGarage implements Garage {
private ArrayList<Car> myGarage;
public MyGarage() {
myGarage = new ArrayList<Car>();
}
@Override
//Adds a Car if the registration is unique
public boolean add(Car c) {
for(Car car : myGarage) {
if(car.getRegistration().equals(c.getRegistration())) {
System.out.println("Car has the same Registration as another illegal");
return false;
}
}
myGarage.add(new Car(c.getOwner(),c.getRegistration(),c.getMake(),c.getkilometres(), c.getprice()));
return true;
}
@Override
public Car getCar(String carID) {
for(Car car : myGarage) {
if(carID.equals(car.getRegistration())) {
System.out.println("Car Found");
return car;
}
}
System.out.println("No car of that record");
return null;
}
@Override
public ArrayList<Car> getMake(String make) {
ArrayList<Car> carModel = new ArrayList<Car>();
for(Car car : myGarage) {
if (car.getMake().equals(make)) {
carModel.add(car);
}
}
System.out.println(carModel.toString());
return carModel;
}
@Override
public void totalValue() {
double amount = 0;
for(Car car : myGarage) {
amount = car.getprice() + amount;
}
System.out.println("The total amount is: " + amount);
}
@Override
public boolean changeOwner(String registration, String ow) {
for(Car car : myGarage) {
if(car.getRegistration().equals(registration)) {
myGarage.remove(car);
car = new Car(ow, "444","F-50", 4, 4000.99);
myGarage.add(car);
return true;
}
}
return false;
}
}
最佳答案
In object-oriented and functional programming, an immutable object (unchangeable object) is an object whose state cannot be modified after it is created. This is in contrast to a mutable object (changeable object), which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change, but the object's state appears unchanging from an external point of view. - WikiPedia
因此,不可变对象(immutable对象)是其状态在初始化后不会改变的实例。这些类型的类通常适用于需要实现某种形式的缓存以及您担心多线程环境中的线程安全性的应用程序(不可变对象(immutable对象)本质上是线程安全的)。
我没有看到您的 Car
类,但假设它看起来像这样:
public final class Car {
final String registration;
final String owner;
public Car(String registration, String owner) {
this.registration = registration;
this.owner= owner;
}
public String getRegistration() {
return registration;
}
public String getOwner() {
return owner;
}
}
...请注意,此类中没有 setter
方法。因此,car
只能被初始化(即 Car myCar = new Car("abcd", "John");
以及其中的变量(即 registration
和 owner
)永远无法更新。
因此,您的 changeOwner
方法本质上是循环遍历 garage
中的 car
实例,当它找到匹配的注册号时,它会删除该注册号从您的车库
中获取汽车
实例,然后添加一辆全新的汽车。
为了演示这一点,您可以运行以下命令:
public class Garage {
public static void main(String ... args) {
List<Car> myGarage = new ArrayList<>();
myGarage.add(new Car("CG404GH", "John"));
System.out.println(myGarage);
for(Car car : myGarage) {
if("CG404GH".equals(car.getRegistration())) {
myGarage.remove(car);
Car updateCar = new Car("DD404GH", "John");
myGarage.add(updateCar);
}
}
System.out.println(myGarage);
}
}
这将打印出类似于以下内容的内容(@后面的部分每次运行都会不同):
[Car@4411d970]
[Car@6442b0a6]
这里需要注意的是,@后面的值是不同的,因此它们是car
的两个完全不同的类(实例)
关于java - 你能改变一个不可变的类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58794252/