有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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

所以我的代码是:

教程。爪哇

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; } }

沃勒松。爪哇

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的值。讲师姓名

为什么我不能这样做

我感谢你的帮助 谢谢


共 (3) 个答案

  1. # 1 楼答案

    将settName更改为

    public void settName(String name) {
        this.tName = name;
    }
    
  2. # 2 楼答案

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

    在本例中,是一个字符串。因此,您需要将方法更改为:

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

    在这里阅读关于什么是java方法以及如何创建java方法的更多信息:http://www.tutorialspoint.com/java/java_methods.htm

  3. # 3 楼答案

    因为你的目标是:

    My goal is to set the value of tName equal the value of 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;
    }