gpt4 book ai didi

java - java中带输入的多个构造函数的基本用法

转载 作者:行者123 更新时间:2023-11-30 06:48:20 24 4
gpt4 key购买 nike

尽管对构造函数进行了研究,但我还是无法理解它们,非常感谢一些帮助和解释。

我的目标是让用户输入汽车的“品牌”和“型号”,我希望将输入分配给构造函数中的每个变量,然后再拥有三辆不同的汽车。

如何正确有效地设置一个“make”变量,该变量分配给用户输入并可以使用构造函数进行访问以进行显示。这是非常基本的,但我在编码时遇到了困难。

package cartest;

import java.util.Scanner;

public class CarTest
//make, model, year, price, color, size
//default constructor
//property assignment constructor
//enter three diferent cars and display final information
//toString() method and a method to honk the horn
{
String make;
String model;
int year;
int price;
String color;
String size;

public CarTest()
{

}

void setMake(String make)
{
Scanner scanner = new Scanner (System.in);
System.out.println("Please enter a make:");
make = scanner.nextLine();
}

void getMake()
{
System.out.println("Selected make: " + make);
}
void setModel(String model)
{
Scanner scanner = new Scanner (System.in);
System.out.println("Please enter a model:");
model = scanner.nextLine();
}



public static void main(String[] args)
{

CarTest carOne = new CarTest();
carOne.getMake();



}


}

最佳答案

I'm aiming to have the user input a "make" and a "model" for a car, I would like the assignment of input to each variable in a constructor, and later on have three different cars.

我对你的类(class)做了一些修改,如下所示:

class CarTest {
private String make;
private String model;
private int year;
private int price;
private String color;
private String size;

public CarTest() {
this.make = "somevalue";
this.model = "somevalue";
this.year = 2017;
this.price = 50000;
this.color = "somecolor";
this.size = "somesize";
}

public CarTest(String make, String model){
this.make = make;
this.model = model;
}


public String getMake() {
return make;
}

public void setMake(String make) {
this.make = make;
}

public String getModel() {
return model;
}

public void setModel(String model) {
this.model = model;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

public String getSize() {
return size;
}

public void setSize(String size) {
this.size = size;
}

@Override
public String toString() {
return "CarTest{" +
"make='" + make + '\'' +
", model='" + model + '\'' +
", year=" + year +
", price=" + price +
", color='" + color + '\'' +
", size='" + size + '\'' +
'}';
}
}

正如您所看到的,有一个默认构造函数,您可以在其中指定对象的默认值,还有一个带有两个参数的构造函数,允许您设置 makemodel 在对象构造时,如果您认为有必要,当然可以扩展参数。

我建议将 main 方法分离到不同的类中并让它执行此功能:

public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
System.out.println("Please enter a model:");
String model = scanner.nextLine(); // validate if you need to
System.out.println("please enter a make");
String make = scanner.nextLine(); // validate if you need to
CarTest carOne = new CarTest(make,model); // pass the entered details to the carOne object
System.out.println(carOne.getMake()); // will display the value of make that has been entered
}

我已经为你完成了一半的任务:

//make, model, year, price, color, size
//default constructor
//property assignment constructor
//enter three diferent cars and display final information
//toString() method and a method to honk the horn

您只需在我提供的解决方案中添加一些额外的代码,然后就可以开始了。

关于java - java中带输入的多个构造函数的基本用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43289136/

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