gpt4 book ai didi

java - 用户定义函数的困难

转载 作者:行者123 更新时间:2023-12-02 02:06:45 24 4
gpt4 key购买 nike

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

最佳答案

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

关于java - 用户定义函数的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50649368/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com