Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我已经编写了构造函数类,但是我需要测试它。我该怎么做?_Java_Class Constructors - Fatal编程技术网

Java 我已经编写了构造函数类,但是我需要测试它。我该怎么做?

Java 我已经编写了构造函数类,但是我需要测试它。我该怎么做?,java,class-constructors,Java,Class Constructors,我是一个相对较新的Java程序员,我正在学习构造函数。我已经掌握了如何使构造函数本身停止工作的格式,但是我的计算机科学老师要求我再写几行代码,以确保构造函数正常工作 我看过其他网站,但它并没有真正给我我所需要的 我曾尝试使用我认为在逻辑上可行的方法(键入“a.variable()”作为对象),但也不起作用 class Car { public String make; public String model; public int numberOfDoors; p

我是一个相对较新的Java程序员,我正在学习构造函数。我已经掌握了如何使构造函数本身停止工作的格式,但是我的计算机科学老师要求我再写几行代码,以确保构造函数正常工作

我看过其他网站,但它并没有真正给我我所需要的

我曾尝试使用我认为在逻辑上可行的方法(键入“a.variable()”作为对象),但也不起作用

class Car {
    public String make;
    public String model;
    public int numberOfDoors;
    public int topSpeed;
    public int price;

    Car(String make, String model, int numberOfDoors, int topSpeed, int price){
        this.make = make;
        this.model = model;
        this.numberOfDoors = numberOfDoors;
        this.topSpeed = topSpeed;
        this.price = price;
    }

    Car(String make, String model, int topSpeed, int price){
        this.make = make;
        this.model = model;
        this.numberOfDoors = 4;
        this.topSpeed = topSpeed;
        this.price = price;
    }

    Car(int numberOfDoors, int topSpeed, int price){
        this.make = "unknown";
        this.model = "unknown";
        this.numberOfDoors = numberOfDoors;
        this.topSpeed = topSpeed;
        this.price = price;
    }

    Car(String make, String model, int numberOfDoors){
        this.make = make;
        this.model = model;
        this.numberOfDoors = numberOfDoors;
        this.topSpeed = 90;
        this.price = 0;
    }
}
我正在寻找可以打印出如下内容的内容:


1990野马,4门,140英里/小时,40000美元,你需要做的就是使用合适的构造函数创建一个类
汽车的实例

public class Example {
    public static void main(String[] args) {
        Car car = new Car("Mustang", "1990", 4, 140, 40000);
    }
}
创建实例
car
后,您可以访问其字段

比如说,

int numberOfDoors = car.numberOfDoors;
我们通常将字段设置为私有,并通过getter访问它们:

int numberOfDoors = car.getNumberOfDoors();
假设有一个方法
getNumberOfDoors
定义为

public int getNumberOfDoors() {
    return this.numberOfDoors;
}

您需要做的就是使用适当的构造函数创建类
Car
的实例

public class Example {
    public static void main(String[] args) {
        Car car = new Car("Mustang", "1990", 4, 140, 40000);
    }
}
创建实例
car
后,您可以访问其字段

比如说,

int numberOfDoors = car.numberOfDoors;
我们通常将字段设置为私有,并通过getter访问它们:

int numberOfDoors = car.getNumberOfDoors();
假设有一个方法
getNumberOfDoors
定义为

public int getNumberOfDoors() {
    return this.numberOfDoors;
}

在任何人开口之前,是的,我知道我在将代码和内容转换为StackOverflow时犯的技术错误…要访问对象的公共字段,语法是
a.variable
,而不是
a.variable()
。括号用于调用方法,而您的类没有方法。您应该(通常)只有一个构造函数实际执行实际工作:其他重载应该只调用
this(..)
,转发参数并为省略的参数提供默认值。“我知道我在将代码和内容转换为StackOverflow时犯的技术错误”如果你知道的话,你能修复它们吗?在别人说什么之前,是的,我知道我在将代码和东西转换为StackOverflow时犯的技术错误…要访问对象的公共字段,语法是
a.variable
,而不是
a.variable()
。括号用于调用方法,而您的类没有方法。您应该(通常)只有一个实际执行实际工作的构造函数:其他重载应该只调用
this(…)
,转发参数并为省略的参数提供默认值。“我知道我在将代码和内容转换为StackOverflow时出现的技术错误”如果你知道,你能修复它们吗?