用户定义函数的难度

时间:2018-06-01 18:43:19

标签: java

我是Java的新手并且尝试学习一些简单的Java但是在构造函数方面遇到了一些困难。似乎我设置的构造函数和属性不是由来自Cars类的Main Java类获取的。即使我在主类中使用“汽车”来引用。它还显示“无法解决符号....”这就是我得出结论的方式。可能是什么导致了这个?

Car.java文件

import java.awt.*;

public class Car {

    double averageMilesPerGallon;
    String licensePlate;
    Color paintColor;
    boolean areTailingWorking;

    public Car(double inputAverageMPG,
               String inputLicensePlate,
               Color inputPaintColor,
               boolean inputAreTaillightsWorking) {
        this.averageMilesPerGallon = inputAverageMPG;
        this.licensePlate = inputLicensePlate;
        this.paintColor = inputPaintColor;
        this.areTailingWorking = inputAreTaillightsWorking;

    }
}

Main.java文件

public class Main {

    public static void main(String[] args) {


        Car myCar = new Car(inputAverageMPG: 25.5,
                inputLicensePlate:"1B32E",
                Color.BLUE,
                inputAreTaillightsWorking: true);

        Car sallyCar = new Car(inputAveraMPG: 13.9,
                inputLicensePlate: "1G42D",
                Color.BLACK.
                        inputAreTaillightsWorking: false);

        System.out.println("My car's license plate: " + myCar.licensePlate);
        System.out.println("Sally's License Plate: " + sallyCar.licensePlate);
    }
}

1 个答案:

答案 0 :(得分:2)

Java没有命名参数。而不是这样做

    Car myCar = new Car(inputAverageMPG: 25.5,
            inputLicensePlate:"1B32E",
            Color.BLUE,
            inputAreTaillightsWorking: true);

    Car sallyCar = new Car(inputAveraMPG: 13.9,
            inputLicensePlate: "1G42D",
            Color.BLACK.
                    inputAreTaillightsWorking: false);

这是无效的Java代码,只是传入参数,没有任何前缀或类似的东西。

    Car myCar = new Car(25.5, "1B32E", Color.BLUE, true);
    Car sallyCar = new Car(13.9, "1G42D", Color.BLACK, false);