我在理解构造函数时遇到困难,尽管对它们进行了研究,但是我们非常感谢一些帮助和解释。
我的目标是让用户为汽车输入“make”和“model”,我想在构造函数中为每个变量分配输入,后来又有三辆不同的汽车。
如何正确有效地设置分配给用户输入的“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();
}
}
答案 0 :(得分:1)
我的目标是让用户输入" make"和"模型"对于一辆车,我 想在构造函数中为每个变量赋值, 后来有三辆不同的汽车。
我已经修改了你的课程,如下:
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 + '\'' +
'}';
}
}
你可以看到有一个默认构造函数,你可以在其中指定对象的默认值,还有一个构造函数,它带有2个参数,允许你在对象上设置make
和model
如果您认为有必要,可以扩展参数。
我建议将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
您只需要在我提供的解决方案中添加一些额外的代码,然后您就可以了。