2014-09-27 74 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 
    } 
}