2015-08-08 79 views
0
public abstract class Car { 
    // This class includes common properties for a car, in this way we wont have to change if we need to add a new car brand 
     public String name; 
     public String colour; 
     public int model; 
     public String feature; 
     public String getFeature() { 
      return feature; 
     } 
     public void setFeature(String feature) { 
      this.feature = feature; 
     } 
     public String getName() { 
      return name; 
     } 
     public void setName(String name) { 
      this.name = name; 
     } 
     public String getColour() { 
      return colour; 
     } 
     public void setColour(String colour) { 
      this.colour = colour; 
     } 
     public int getModel() { 
      return model; 
     } 
     public void setModel(int model) { 
      this.model = model; 
     } 
    } 

Test.java爲什麼toString方法在覆蓋時不起作用?

import java.util.Scanner; 

public class Test { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     CarFactory carfactory = new CarFactory(); 

     System.out.println("Hello, please enter your car brand \n BMW \n MERCEDE \n OPEL"); 
     Car usercar = null; 


     String usertext = input.nextLine(); 
     usercar = carfactory.makeCar(usertext); 
     System.out.println("enter colour of your car"); 
     usertext = input.nextLine(); 
     usercar.setColour(usertext); 
     System.out.println("enter model of your car"); 
     usertext =input.nextLine(); 
     usercar.setModel(Integer.parseInt(usertext)); 

     System.out.println("Your Car Information;\n "+ usercar.getName()+" \n Colour:" + usercar.getColour() + "\n Model "+ usercar.getModel()+ "\n Your car's plus point is " + usercar.getFeature()); 

     } 

的問題是,如果我要打印汽車信息與的toString梅託德,怎麼會是誰?我寫了一個在汽車類,但是它沒有工作,特點是從汽車自身的類分配..

這裏是我的toString梅託德

public String toString(){ 
    return "Your Car Information;\n "+ getName()+" \n Colour:" + getColour() + "\n Model "+getModel()+ "\n Your car's plus point is " +getFeature(); 
} 
+1

請重新格式化您的代碼 - 縮進每個代碼行至少4個空格,否則幾乎不可讀。另外 - 你想要toString()做什麼exacly? –

+1

「*如果我想用toString Metod打印汽車信息,它將如何?*」您只需使用'System.our.println(yourCar)'。 'println'將在內部調用'toString'方法並使用它的結果。它類似於'System.our.println(yourCar.toString())' – Pshemo

+1

「*如果我想用toString Metod打印汽車信息*」 - 你的'toString'方法不應該打印任何東西。它應該簡單地返回一個字符串。如果要打印該字符串,請使用@Pshemo建議的「System.out.println(carObject)」。 – aioobe

回答

1

首先,你必須覆蓋的java.lang.ObjecttoString()方法是這樣的:

class Car { 
    ... 
    @Override 
    public String toString() { 
     return "Your Car Information;\n " + getName() + " \n Colour:" + 
       getColour() + "\n Model " + getModel() + "\n Your car's plus point is " + 
       getFeature(); 
    } 
} 

其次,你可以使用它像這樣:

public static void main(String[] args) { 
    // 'CarClass' is a no-abstract class, who extends the 'Car' class 
    Car car = new CarClass(); 
    // the first way 
    String information = car.toString(); 
    System.out.println(information); 
    // the second way 
    System.out.println(car); 
}