2014-09-27 39 views
0
public class Student { 
    int marks; 
    String name; 
    char sex; 
    String email; 
} 
Student[] s = new Student[10]; 

public class StudentDemo { 
    Student s[] = new Student[10];// array Student// 
    Student s1 = new Student();// Student Object// 
    s1.setName("John"); //Eclipse says here there is a mistake an ask to delete John// 
    Student[0]=s1; 
} 

我创建了一个具有名称和其他属性的Student类。但是现在我想用Student对象初始化数组的每个元素。这段代码是否正确? Eclipse会抛出很多红点。 帮助。用Student对象初始化数组中的每个元素

+0

你需要在一些方法里面写这段代码。你不能直接在类中写逻辑。 – 2014-09-27 07:53:23

+0

使用s [0] = s1;而不是Student [0] = s1 ;. – KernelPanic 2014-09-27 07:53:53

回答

0
class Student { 
    int marks; 
    String name; 
    char sex; 
    String email; 
    public void setName(String string) 
    { 
     // TODO Auto-generated method stub 

    } 
} 


public class StudentDemo{ 
    public static void main(String [] args) 
    { 
    Student s[] = new Student[10];// array Student// 
    Student s1 = new Student();// Student Object// 
    s1.setName("John"); //Eclipse says here there is a mistake an ask to delete John// 
    s[0]=s1; 
    } 
} 

试试这个。
问题在你的代码:

  1. 你写的功能之外的功能逻辑。使用main方法修正了我的 代码。
  2. 在类文件中不能有2个公共类。所以我让学生档案非公开。
  3. 你没有为学生的姓名属性设置setter。
0

那么你从来没有定义过setName方法,所以我假设这就是为什么你得到编译器错误。这样的事情应该Student类里面工作

public String setName(String name){ 
      this.name = name; 
    } 
+0

我定义了所有的setter和getters – Jithu 2014-09-27 07:56:01

+0

我认为你应该阅读Marko的评论。 – committedandroider 2014-09-27 07:56:40

0

使用您所创建的,而不是阵列 的类型,因此数组的参考,与s[0]

0

取代Student[0]很多是你的代码错误。

应该

Student[] s = new Student[10]; 
s[0] = new Student(); 
s[0].setName(); 

您还需要写一个方法里面的代码。像这样:

public void doStuffHere() 
{ 
    // Code goes here. 
} 

请注意我用的是事实,在0位置有一个Student对象,然后我刚才设置的名称。没有真正的理由使用s1

0

有几件事情:

首先,你的第一个数组应该这样写:

Student[] s = new Student[10]; 

其次,你永远在你的Student类中定义的方法setName(String name)。这将是这个样子:

public void setName(String name) 
{ 
    this.name = name; 
} 

最重要的是,你不能只是调用的类中的方法,它需要去的方法,构造函数或初始化块中。

例如:

public class StudentDemo 
{ 
    Student[] studentArray = initStudentArray(); 

    private Student[] initStudentArray() 
    { 
     Student[] ret = new Student[10]; 
     Student s = new Student(); 
     s.setName("John"); 
     ret[0] = s; 

     ... 

     return ret; 
    } 
} 
0

这可以帮助你。

class Student { 
    int marks; 
    String name; 
    char sex; 
    String email; 

    void setName(String name){ 
     this.name = name; //this.name represents the current instance non-static variable 
    } 
    public String toString(){ //Overridden Objectclass method for string representation of data 
     return " Student Name: "+name+ 
      "\n Gender: "+sex+ 
      "\n Email: "+email+ 
      "\n Marks: "+marks; 
    } 
} 

public class StudentDemo { 

    public static void main(String[] args){ 
      Student s[] = new Student[10]; 
      s[0] = new Student(); 
      s[0].setName("John"); //similarly u can set values to other attributes of this object 
      System.out.println(s[0]); // same as s[0].toString() its an implicit call 
    } 
}