我是Java编程的新手,我想在运行时动态创建Java中的对象,我已经检查了表单并尝试了一些代码,但似乎没有什么工作。
这是我的代码..非常感谢所有帮助:)
import java.util.Scanner;
public class Main{
public static void main(String[] args){
String carName;
String carType;
String engineType;
int limit;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of Cars you want to add - ");
limit = in.nextInt();
for(int i = 0; i <limit; i++){
Cars cars[i] = new Cars();
System.out.print("Enter the number of Car Name - ");
carName = in.nextLine();
System.out.print("Enter the number of Car Type - ");
carType = in.nextLine();
System.out.print("Enter the Engine Type - ");
engineType = in.nextLine();
cars[i].setCarName(carName);
cars[i].setCarType(carType);
cars[i].setEngineeSize(engineType);
String a = cars[i].getCarName();
String b = cars[i].getCarType();
String c = cars[i].getEngineeSize();
System.out.println(a,b,c);
}
}
}
汽车类看起来像这样......
public class Cars{
public String carName;
public String carType;
public String engineeSize;
public void Cars(){
System.out.println("The Cars constructor was created ! :-) ");
}
public void setCarName(String cn){
this.carName = cn;
}
public void setCarType(String ct){
this.carType = ct;
}
public void setEngineeSize(String es){
this.engineeSize = es;
}
public String getCarName(){
return this.carName;
}
public String getCarType(){
return this.carType;
}
public String getEngineeSize(){
return this.engineeSize;
}
}
答案 0 :(得分:0)
你走在正确的轨道上,虽然有一些错误和不必要的位。
汽车类
你的汽车课程大多都很好,(虽然在我看来Car
会更有意义)但是你的构造函数没有意义,你有public void Cars()
,void
意思是“这个方法什么都不返回“,但你想返回一个Cars
对象,这意味着你的构造函数需要变成:
public Cars()
{
System.out.println("The Cars constructor was created ! :-) ");
}
您的主要班级
您也非常接近,主要问题是创建cars
数组limit
次:
for(int i = 0; i < limit; i++)
{
Cars cars[i] = new Cars();
//Other code
}
数组需要在for
循环之外进行。
以下是修订后的Main
课程,评论应该很好地解释我的所作所为以及为什么。
import java.util.Scanner;
public class Main{
public static void main(String[] args){
//The strings here were unnecessary
int limit;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of Cars you want to add - ");
limit = in.nextInt();
in.nextLine(); //nextInt leaves a newLine, this will clear it, it's a little strange, but it makes sense seeing as integers can't have newlines at the end
//Make an array of Cars, the length of this array is limit
Cars[] cars = new Cars[limit];
//Iterate over array cars
for(int i = 0; i < limit; i++)
{
//Read all the properties into strings
System.out.println("Enter the number of Car Name - ");
String carName = in.nextLine();
System.out.println("Enter the number of Car Type - ");
String carType = in.nextLine();
System.out.println("Enter the Engine Type - ");
String engineType = in.nextLine();
//Set the object at current position to be a new Cars
cars[i] = new Cars();
//Adjust the properties of the Cars at this position
cars[i].setCarName(carName);
cars[i].setCarType(carType);
cars[i].setEngineeSize(engineType);
//We still have the variables from the scanner, so we don;t need to read them from the Cars object
System.out.println(carName+carType+engineType);
}
in.close(); //We don't need the scanner anymore
}
}
完成打字并意识到这个问题是两年了:)