Java 使用对象数组调用构造函数
我需要创建一个对象数组,并从控制台读取构造函数中元素的值。我完全搞不懂怎么做。有人能说清楚怎么做吗Java 使用对象数组调用构造函数,java,Java,我需要创建一个对象数组,并从控制台读取构造函数中元素的值。我完全搞不懂怎么做。有人能说清楚怎么做吗 public class Student { int id; String name; double marks; public Student(int id, String name, double marks) { id = this.id; name = this.name; marks = this.mark
public class Student {
int id;
String name;
double marks;
public Student(int id, String name, double marks) {
id = this.id;
name = this.name;
marks = this.marks;
}
}
public class Solution {
public static void main(String[],args)
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
Student[] arr=new Student[n];
for(int i=0;i<n;i++)
{
int x =sc.nextInt();
String y=sc.nextLine();
double z=sc.nextDouble();
arr[i]=arr.Student(x,y,z);
}
}
}
公共班级学生{
int-id;
字符串名;
双标记;
公立学生(整数id、字符串名称、双标记){
id=this.id;
name=this.name;
马克斯=这个。马克斯;
}
}
公共类解决方案{
公共静态void main(字符串[],args)
{
扫描仪sc=新的扫描仪(System.in);
int n=sc.nextInt();
学生[]arr=新学生[n];
对于(int i=0;i您可以执行以下两种操作之一:
1.通过调用构造函数并将该对象添加到数组中来创建临时对象:
Student temp= new Student(x,y,z);
arr[i]=temp;
2.直接实例化一个新对象并将其添加到数组中,如下所示:
arr[i]=new Student(x,y,z);
这两种方法都可以很好地工作,但建议使用方法2,因为您不应该将内存分配给临时对象,因为您显然可以在不这样做的情况下实现目标。您可以做以下两件事之一:
1.通过调用构造函数并将该对象添加到数组中来创建临时对象:
Student temp= new Student(x,y,z);
arr[i]=temp;
2.直接实例化一个新对象并将其添加到数组中,如下所示:
arr[i]=new Student(x,y,z);
这两种方法都可以很好地工作,但建议使用方法2,因为您不应该将内存分配给临时对象,因为您显然可以在不这样做的情况下实现目标,而不是:
arr[i]=arr.Student(x,y,z)
做:
arr[i]=新生(x,y,z)
为什么?因为数组中的每个对象都是学生类的实例,而不是:
arr[i]=arr.Student(x,y,z)
做:
arr[i]=新生(x,y,z)
为什么?因为数组中的每个对象都是Student类的实例,因为您的构造函数是错误的
public class Student {
int id;
String name;
double marks;
public Student(int id, String name, double marks) {
this.id = id;
this.name = name;
this.marks = marks;
}
}
因为你的构造函数是错误的
public class Student {
int id;
String name;
double marks;
public Student(int id, String name, double marks) {
this.id = id;
this.name = name;
this.marks = marks;
}
}
构造函数声明错误。此
始终用于引用实例变量。请将构造函数更改为:
public class Student {
int id;
String name;
double marks;
public Student(int id, String name, double marks) {
this.id = id;
this.name = name;
this.marks = marks;
} }
public class Solution {
public static void main(String[],args)
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
Student[] arr=new Student[n];
for(int i=0;i<n;i++)
{
int x =sc.nextInt();
String y=sc.nextLine();
double z=sc.nextDouble();
arr[i]= new Student(x,y,z); //no need to create an object for arr
}
}
}
公共班级学生{
int-id;
字符串名;
双标记;
公立学生(整数id、字符串名称、双标记){
this.id=id;
this.name=名称;
这个。标记=标记;
} }
公共类解决方案{
公共静态void main(字符串[],args)
{
扫描仪sc=新的扫描仪(System.in);
int n=sc.nextInt();
学生[]arr=新学生[n];
对于(int i=0;i您的构造函数声明错误。此
始终用于引用实例变量。请将您的构造函数更改为:
public class Student {
int id;
String name;
double marks;
public Student(int id, String name, double marks) {
this.id = id;
this.name = name;
this.marks = marks;
} }
public class Solution {
public static void main(String[],args)
{
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
Student[] arr=new Student[n];
for(int i=0;i<n;i++)
{
int x =sc.nextInt();
String y=sc.nextLine();
double z=sc.nextDouble();
arr[i]= new Student(x,y,z); //no need to create an object for arr
}
}
}
公共班级学生{
int-id;
字符串名;
双标记;
公立学生(整数id、字符串名称、双标记){
this.id=id;
this.name=名称;
这个。标记=标记;
} }
公共类解决方案{
公共静态void main(字符串[],args)
{
扫描仪sc=新的扫描仪(System.in);
int n=sc.nextInt();
学生[]arr=新学生[n];
对于(inti=0;iarr[i]=newstudent(x,y,z);
此外,您在构造函数中设置的变量不正确:id=this.id;
应该是this.id=id;
(其他两个变量相同)arr[i]=newstudent(x,y,z);
此外,您在构造函数中设置的变量不正确:id=this.id;
应该是this.id=id;
(其他两个变量也是如此)