Java (需要标识符)getter/setter和对象
我的程序有问题。当我尝试编译以下内容时,我只收到消息:Java (需要标识符)getter/setter和对象,java,object,setter,getter,Java,Object,Setter,Getter,我的程序有问题。当我尝试编译以下内容时,我只收到消息: Tutorium.java:15: error: <identifier> expected public void settName(vorlesung.lectureName) { ^ java public class Vorlesung { public String lectureName; private int l
Tutorium.java:15: error: <identifier> expected
public void settName(vorlesung.lectureName) {
^
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;
}}
我的主要方法是:
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.讲师名称的值
为什么我不能这样做
我感谢你的帮助
感谢方法,您传入的参数必须具有声明值 在本例中,是一个字符串。因此,您需要将方法更改为:
public void settName(String newLectureName) {
this.tName = newLectureName;
}
在此处阅读有关java方法是什么以及如何创建java方法的更多信息:将settName更改为
public void settName(String name) {
this.tName = name;
}
因为你的目标是:
我的目标是将tName的值设置为vorlesung.讲师名称的值
您应该完全摆脱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;
}
settName(vorlesung.讲师名){
?这是想做什么?当你把那个奇怪的方法参数放在那里时,你脑子里想了些什么——它是什么?不应该改为settName(字符串名){
?相信我,它的效果比你的好得多。你的参数声明不是有效的Java。你需要显示该尝试,因为你刚才发布的错误消息与我提到的代码不匹配。解释“不起作用”
请。这不是很有用,也没有为我们提供足够的信息来帮助您。请不要编辑您的问题,因为这样会使以前版本的问题的现有答案无效。@peter87:应该是get讲师()
。这是Vorlesung类中的一个方法。最重要的是,您的类需要一个setVorlesung(…)
方法。
public void setVorlesung(Vorlesung vorlesung) {
this.vorlesung = vorlesung;
}