2014-11-24 168 views
1

我的程序有問題。當我嘗試編譯下面我剛剛收到的消息:(標識符預期)getter/setter和對象

Tutorium.java:15: error: <identifier> expected 
    public void settName(vorlesung.lectureName) { 
              ^

所以我的代碼:

Tutorium.java

public class Tutorium { 
private Vorlesung vorlesung; 
public String tName; 
private int tNumber; 


public int gettNumber() { 
    return this.tNumber; 
} 

public String gettName() { 
    return this.tName; 
} 

public void settName(vorlesung.lectureName) { 
    this.tName = vorlesung.lectureName; 
} 

public String toString() { 
    return (this.tName + ", " + this.tNumber); 
} 

public Tutorium(int tNumber){ 
    this.tNumber = tNumber; } } 

Vorlesung.java

public class Vorlesung { 
public String lectureName; 
private int lectureNumber; 
private int lecture; 
private Dozent dozent; 
private String lecturerlName; 

public String getlectureName(){ 
    return this.lectureName; 
} 

public int lectureNumber(){ 
    return this.lectureNumber; 
} 

public int lecture(){ 
    return this.lecture; 
} 

public String getlecturer(){ 
    this.lecturerlName = dozent.lecturerlName; 
    return this.lecturerlName; 
} 

public String toString() { 
    return (this.lectureName + ", " + this.lectureNumber); 
} 

public Vorlesung(String lectureName, int lecture) { 
    this.lectureName = lectureName; 
    this.lecture = lecture +1; 
    this.lectureNumber = this.lecture -1; 
    this.lecturerlName = lecturerlName; 
}} 

我Main-方法:

public class MainVorlesung { 
public static void main(String[] args) { 
    Student student = new Student("STUDENTNAME", "STUDENTLASTNAME", 178, 1); 
    Vorlesung vorlesung = new Vorlesung("Programmieren", 13341); 
    Tutorium tutorium = new Tutorium(3); 
    Dozent dozent = new Dozent("LECTURERFIRSTNAME", "LECTURERLASTNAME", 815); 

    System.out.println(student.toString()); 
    System.out.println(vorlesung.toString()); 
    System.out.println(tutorium.toString()); 
    System.out.println(dozent.toString()); 

}} 

我的目標是設置tName的值等於vorlesung.lectureName的值。

爲什麼我不能這樣做?

我很感激每一個幫助。 :) 謝謝

+0

'settName(vorlesung.lectureName){'??這是試圖做什麼?當你將那個奇怪的方法參數放在那裏時,你有什麼想法 - 它是什麼?不應該是'settName(String name){'? – 2014-11-24 00:32:53

+0

它比你在裏面有更好的效果,相信我。您的參數聲明不是有效的Java。您需要顯示該嘗試,因爲您剛發佈的錯誤消息與我提到的代碼不匹配。 – 2014-11-24 00:34:57

+1

請解釋'「不起作用」。這不是非常有用,也不會給我們足夠的信息來幫助我們。 – 2014-11-24 00:36:15

回答

0

因爲你的目標是:

我的目標是設置tName的值等於vorlesung.lectureName的值。

你應該完全擺脫setName方法,因爲它完全取決於vorlesung字段,所以不應該改變。你也應該擺脫TNAME領域,而是改變getName()到:

public class Tutorium { 
    private Vorlesung vorlesung; 
    // public String tName; // get rid of 
    private int tNumber; 



    public String gettName() { 
     if (vorlesung != null) { 
      return vorlesung.getlecturer(); 
     } 

     return null; // or throw exception 
    } 

    // *** get rid of this since you won't be setting names 
    // public void settName(Vorlesung vorlesung) { 
    //  this.tName = vorlesung.lectureName; 
    // } 

我剛纔注意到你Tutorium類沒有絕對需要一個setVorlesung(...)方法。

public void setVorlesung(Vorlesung vorlesung) { 
    this.vorlesung = vorlesung; 
} 
+0

@ peter87:它應該閱讀'getlecturer()'。這是Vorlesung課程的一種方法。最重要的是,你需要爲你的課程設置一個'setVorlesung(...)'方法。 – 2014-11-24 01:02:22

1

對於方法,您傳入的參數必須具有聲明的值。

在這種情況下,一個字符串。所以,你需要改變你的方法是:

public void settName(String newLectureName) { 
    this.tName = newLectureName; 
} 

瞭解更多關於什麼是Java方法,以及如何在這裏創建一個:http://www.tutorialspoint.com/java/java_methods.htm

1

變化settName到

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