public void welcome() {
System.out.println("Welcome to Car Rental System!");
System.out.println("Rent a car? 1.Yes 2.No");
}
主流程直接調用,無數據流入流出。
public void printCars(Car[] cars) {
System.out.printf("No.\tBrand\tPrice\tCapacity%n");
for (int i = 0; i < cars.length; i++) {
System.out.printf("%d\t%s\t%d\t%d%n", i+1, cars[i].getBrand(), cars[i].getPrice(), cars[i].getCapacity());
}
}
接收外部數據(引用傳遞),負責格式化輸出,不產生新數據返回。
public int calcPrice(Car[] cars, int index) {
return cars[index].getPrice();
}
接收數據,經過業務邏輯加工,返回結果供調用者使用(如打印賬單)。
**Java 只有值傳遞。基本類型傳值拷貝;引用類型傳引用(地址)拷貝。**

public class ArgsTransfer {
public static void main(String[] args) {
ArgsTransfer t = new ArgsTransfer();
int a = 5, b = 10;
System.out.println("a=" + a + " b=" + b + " ret=" + t.swap(a, b));
// 輸出:a=5 b=10 ret=10
}
public int swap(int i, int j) {
int temp = i; i = j; j = temp; // 僅交換局部拷貝
return i; // 返回 10
}
}
a,b 不變(值拷貝)。10 來自方法內局部變量 i。public void modify(Model m) {
m.setValue(2); // 修改原對象內容 → 外部可見
m = new Model(); // 重新賦值局部引用 → 外部不可見
m.setValue(3); // 修改新對象
}
若多線程共享同一可變對象 → 數據競爭。**解決:深拷貝**。
Cloneable + 重寫 clone()(注意引用字段需遞歸 clone)。new Model(src.getValue())。calc/get/print/save)。fillData(dto))。