java 用java创建汽车类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26092179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:16:13  来源:igfitidea点击:

Creating a car class in java

java

提问by Kyle

Okay, I need to write code that makes this file

好的,我需要编写代码来创建这个文件

public class HW1tester
{
    public static void main(String[] args)
    {    
        Car car1 = new Car();
        Car car2 = new Car("Ford", 2013, 20000);
        Car car3 = new Car("Audi", 2012, 25000);
        Car car4 = new Car();

        car2.setPrice(22000);
        car2.setYear(2011);

        car4.setBrand("Cadillac");

        System.out.println("This car is " + car1.getBrand() + ", year " + car1.getYear() + ", price " + car1.getPrice());
        System.out.println("This car is " + car2.getBrand() + ", year " + car2.getYear() + ", price " + car2.getPrice());
        System.out.println("This car is " + car3.getBrand() + ", year " + car3.getYear() + ", price " + car3.getPrice());
        System.out.println("This car is " + car4.getBrand() + ", year " + car4.getYear() + ", price " + car4.getPrice());

        System.out.println("The total car number is: " + car1.getNumber());
        System.out.println("The total car number is: " + car2.getNumber());
        System.out.println("The total car number is: " + car3.getNumber());
        System.out.println("The total car number is: " + car4.getNumber());

   }
}

So far I have this, but I'm not sure what the hell I'm doing wrong.

到目前为止,我有这个,但我不确定我到底做错了什么。

public class Car 
{
    private int yearModel;
    private String brand;
    private int priceModel;
    private int numberModel;

    public Car(String b, int year, int price, int number)
    {
        yearModel = year;
        brand = b;
        priceModel = price;
        numberModel = number;      
    }

    public int getYear()                
    {
        return yearModel;
    }

    public String getBrand()
    {
        return brand;
    }

    public int getPrice()
    {
        return priceModel;
    }

    public int getNumber()
    {
        return numberModel;
    }

    public void setYear(int year)
    {
        yearModel = year;
    }

    public void setBrand(String carBrand)
    {
        brand = carBrand;
    }

    public void setPrice(int price)                        
    {
        priceModel = price;   

    public void setNumber(int number)
    {
        numberModel = number;
    }
}

Everytime I run the first code right now it just gives me errors on car1, car2, etc I just can't seem to see what I'm doing wrong at all, I hope somebody can help me out. By the way, I can't make ANY changes to HW1tester.

每次我现在运行第一个代码时,它只会在 car1、car2 等上给我错误,我似乎根本看不出我做错了什么,我希望有人能帮助我。顺便说一句,我无法对 HW1tester 进行任何更改。

回答by gprathour

You have created parameterized Constructor i.e. public Car(String b, int year, int price, int number)

您已经创建了参数化的构造函数,即 public Car(String b, int year, int price, int number)

So when you are trying to create object for the same like, Car car1 = new Car();then it won't be possible. Because in this you are trying to call default constructor. Which is not present in the class.

因此,当您尝试为相同的对象创建对象时,Car car1 = new Car();这是不可能的。因为在这里你试图调用默认构造函数。这在课堂上是不存在的。

While creating object you need to pass 4arguments.

创建对象时,您需要传递4 个参数。

Moreover, in Car car2 = new Car("Ford", 2013, 20000);You are passing 3 arguments which doesn't match with the constructor.

此外,在Car car2 = new Car("Ford", 2013, 20000);You 传递了 3 个与构造函数不匹配的参数。

To create object of class Car, you need to do something like,

要创建 Car 类的对象,您需要执行以下操作,

Car c = new Car('Volvo', 2014, 25000, 1234);

Car c = new Car('Volvo', 2014, 25000, 1234);

回答by P M

When creating a new object (car1, car2, etc.), you're not passing in enough variables. Your constructor requires 4 and you're giving, at most, 3 variables when trying to construct a new car object.

创建新对象(car1、car2 等)时,您没有传入足够多的变量。您的构造函数需要 4 个,并且在尝试构造一个新的汽车对象时,您最多提供 3 个变量。

回答by eno3nt

you need to write overloaded constructors with different parameter sets. when you call new Car(), java is looking for a ctor with no params, new Car("audi", 2013, 25000) one with 3 params etc.

您需要编写具有不同参数集的重载构造函数。当你调用 new Car() 时,java 正在寻找一个没有参数的 ctor,new Car("audi", 2013, 25000) 一个有 3 个参数的 ctor,等等。

in your Car.java file:

在您的 Car.java 文件中:

public Car() {}

then you can set the instance variables with their getters and setters (until you do, their values will be null).

然后您可以使用它们的 getter 和 setter 设置实例变量(在您这样做之前,它们的值将为空)。

if you want, you can define more, but their signatures have to be different. eg: public Car(String b, int year) { ... }and public Car(String b, int price) {...}wont work, because they have the same signature.

如果需要,您可以定义更多,但它们的签名必须不同。例如: public Car(String b, int year) { ... }并且public Car(String b, int price) {...}不会工作,因为它们具有相同的签名。

回答by Kh?i Hoàng Nguy?n

In class Car, you have a constructor that has 4 parameters. However in the main class, you create a Car with 0 or 3 parameters. In other to run the code, you have to add 2 more constructor, one with 0 parameter, and one with 3 parameters.

在 Car 类中,您有一个带有 4 个参数的构造函数。但是在主类中,您创建了一个带有 0 或 3 个参数的 Car。要运行代码,您必须再添加 2 个构造函数,一个带有 0 个参数,一个带有 3 个参数。

public Car() {
}

public Car(String b, int year, int price) {
    yearModel = year;
    brand = b;
    priceModel = price;
}

回答by Er KK Chopra

public Car() {
}

public Car(String b, int year, int price,) {
    yearModel = year;
    brand = b;
    priceModel = price;
}

In that case you have a constructor that has 4 parameters.

在这种情况下,您有一个具有 4 个参数的构造函数。