Вопрос

I seem to be having an issue with regards to my output from a Constructor that I have prepared.

As I am learning Java, I am attempting to familiarize myself with Constructors and their importance.

All I want to do is output a car type.

Here is my code:

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);
    }
}

However, my output is this:

This is your car: Car@5db2381b

Can someone please point out why the output is like this?

Many thanks.

Это было полезно?

Решение 2

First of all your constructor is wrong. That is not how you write a constructor, you pass some params to constructor and those will be assigned to your class variables. So in your case your constructor will be following,

Car(String carMake, int horsePower, String colour, String vehicleType, String interior, int   topSpeed){
    cm = carMake;
    hp = horsePower;
    col = colour;
    vtype = vehicleType;
    intr = interior;
    ts = topSpeed;
}

And for the solution to your problem what you are looking for is overriding the toString method of the class car. which can be done as following.

@Override
public String toString()
{
    StringBuilder sb = new StringBuilder();
    sb.append("Car Make: ");
    sb.append(cm);
    sb.append("\n");   //You can use System.getProperty("line.separator") if you want to make it platform independent 
    sb.append("Car colour: ");
    sb.append(col);
    //You can append as many details of your class car this way.
    //and finally just return the StringBuilder as String. 
    return sb.toString();
}

Другие советы

It is how Java converts an object instance to String by default. By creating the name consisting of class name (Car) and appending @ with some "unique" id. In order to make it more human readable you have to overload the public String toString() method in your class.

Every class you make, inherit from Object class which have some methods and one of them is .toString() which is used to convert a generic Object to a String... it's used for "user" output.

By default

public String toString() {
    return getClass().getName() + '@' + Integer.toHexString(hashCode());
}

it will return the name of the class, a @ and the hashCode of the current object.

To have a great and beautiful output for your class, you should override it.

Example:

@Override
public String toString()
{
    return "My car is a " + cm;
}

(Note: @Override and the same name of the method)

Another problem with your code anyway is the fact that you don't change the fields of the class, but the arguments values (which will be losed after the constructor.)

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;
}

As you can see, you change the value of carMake to cm which is null (because it's a String) so your output will be something of empty and of 0s. Invert it cm = carMake to make it works. (Same for other)

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top