1
我嘗試:對象作爲參數
- 創建於
Main
一個Car
對象。 - 將該對象作爲參數發送到
carOwners
類。 - 從main打印整個
carOwners
數據。
Name
和Address
將被打印,但不是Car
。
我的代碼有什麼問題?
public class TestProgram {
public static void main(String [] args){
Car Saab = createCar();
carOwners guy = createCarOwners (Saab);
printAll(guy);
}
public static Car createCar(){
//user input a, b, c, d
Car temporary = new Car (a, b, c, d);
return temporary;
}
public static carOwners createCarOwners (Car x){
//user input a, b
Car c = x;
carOwners temporary = new carOwners (a, b, c);
return temporary;
}
public static void printAll (carOwners x){
x.printCarData();
x.printNameAddress();
}
}
public class Car {
private String model;
private String Year;
private String licensePlate;
private String color;
public Car (String x, int y, String z, String q){
model = x;
Year = y;
licensePlate = z;
color = q;
}
}
public class carOwners {
private String name;
private String address;
private Car TheCar;
public carOwners (String n, String a, Car b){
name = n;
address = a;
TheCar = b;
}
public void printNameAddress(){
System.out.println();
System.out.println(name);
System.out.println(address);
}
public void printCarData(){
System.out.println(TheCar);
}
}
您將*引用傳遞給對象*到其他方法。 – TheLostMind
覆蓋'Car'類中的'Object#toString'。 – Mena
你應該養成爲你的變量賦予有意義名稱的習慣 – JonK