我似乎有一個關於我已經準備好的構造函數的輸出的問題。構造函數輸出不正確?
當我學習Java時,我試圖熟悉構造函數及其重要性。
我想要做的就是輸出汽車類型。
這裏是我的代碼:
public class Car
{
public String cm;
public int hp;
public String col;
public String vtype;
public String intr;
public int ts;
Car(String carMake, int horsePower, String colour, String vehicleType, String interior, int topSpeed){
carMake = cm;
horsePower = hp;
colour = col;
vehicleType = vtype;
interior = intr;
topSpeed = ts;
}
public static void main (String [] args){
Car carA = new Car("BMW", 2000, "Black", "1 Series", "Leather", 155);
System.out.println("This is your car: " + carA);
}
}
然而,我的輸出是這樣的:
This is your car: [email protected]
可有人請指出爲什麼輸出是這個樣子?
非常感謝。
感謝您的回答,以及對我的構造函數的幫助。非常豐富。 – PrimalScientist
如果它對你有幫助,請做出回覆並接受答案。還問你是否有更多疑問。 – Setu
就個人而言,我會避免在toString中使用StringBuilder,代碼將會更長,並且根本沒有任何改進(編譯器會使用StringBuilder改進它) –