gpt4 book ai didi

java - (需要标识符)getter/setter 和对象

转载 作者:行者123 更新时间:2023-11-30 03:39:28 25 4
gpt4 key购买 nike

我的程序有问题。当我尝试编译以下内容时,我只收到消息:

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

所以我的代码:

教程.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;
}}

我的主要方法:

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 的值。

为什么我不能这样做?

我感谢每一个帮助。 :)谢谢

最佳答案

对于方法,传入的参数必须具有声明的值。

在本例中,是一个字符串。所以你需要将你的方法更改为:

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

在此处详细了解什么是 java 方法以及如何创建方法:http://www.tutorialspoint.com/java/java_methods.htm

关于java - (需要标识符)getter/setter 和对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27096182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com