..
2006-12-28 11:57:01 UTC
※ 引述《paoching (遍地寒霜醉紅塵)》之銘言:
: 有可能是你z宣告的scope錯了....
: 妳試試看在Student x = new Student(z);上一行宣告一下z
: : ----
: 最好還是有code才好幫妳看....
小弟在上一行先宣告Department z = new Department();就可以編譯了。
謝謝各位大大的幫忙。但是我小弟又延生出另一個新的問題,
如果x和z物件互相參考的話怎麼辦?orz....
在MainClass.java這樣寫:
public class MainClass {
public static void main(String[] args) {
Department z = new Department(x);
Student x = new Student(z);
}
}
在Student.java這樣寫:
public class Student {
private Department major;
public Student(Department m){
this.setDepartment(m);
}
public Department getDepartment(){
return major;
}
public void setDepartment(Department m){
major = m;
}
}
在Department.java這樣寫:
public class Department {
private Student studentname;
public Department(Student s){
this.setDepartment(s);
}
public Student getDepartment(){
return studentname;
}
public void setDepartment(Student s){
studentname = s;
}
}
小弟想到的方法是這樣子的,
在Student類別先增一個建構子,
public Student(){};
然後在MainClass類別先宣告一個x物件,使z可以順利建立,
然後再讓x的major屬性參考到z物件。
public class Student {
private Department major;
public Student(){};
public Student(Department m){
this.setDepartment(m);
}
public Department getDepartment(){
return major;
}
public void setDepartment(Department m){
major = m;
}
}
public class MainClass {
public static void main(String[] args) {
Student x = new Student();
Department z = new Department(x);
x.setDepartment(z);
}
}
想請問各位大大有其它的方法嗎?謝謝。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.58.254.254
: 有可能是你z宣告的scope錯了....
: 妳試試看在Student x = new Student(z);上一行宣告一下z
: : ----
: 最好還是有code才好幫妳看....
小弟在上一行先宣告Department z = new Department();就可以編譯了。
謝謝各位大大的幫忙。但是我小弟又延生出另一個新的問題,
如果x和z物件互相參考的話怎麼辦?orz....
在MainClass.java這樣寫:
public class MainClass {
public static void main(String[] args) {
Department z = new Department(x);
Student x = new Student(z);
}
}
在Student.java這樣寫:
public class Student {
private Department major;
public Student(Department m){
this.setDepartment(m);
}
public Department getDepartment(){
return major;
}
public void setDepartment(Department m){
major = m;
}
}
在Department.java這樣寫:
public class Department {
private Student studentname;
public Department(Student s){
this.setDepartment(s);
}
public Student getDepartment(){
return studentname;
}
public void setDepartment(Student s){
studentname = s;
}
}
小弟想到的方法是這樣子的,
在Student類別先增一個建構子,
public Student(){};
然後在MainClass類別先宣告一個x物件,使z可以順利建立,
然後再讓x的major屬性參考到z物件。
public class Student {
private Department major;
public Student(){};
public Student(Department m){
this.setDepartment(m);
}
public Department getDepartment(){
return major;
}
public void setDepartment(Department m){
major = m;
}
}
public class MainClass {
public static void main(String[] args) {
Student x = new Student();
Department z = new Department(x);
x.setDepartment(z);
}
}
想請問各位大大有其它的方法嗎?謝謝。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 210.58.254.254