gpt4 book ai didi

java - 传递一个字符串作为构造函数参数并获取 "illegal start of type"

转载 作者:行者123 更新时间:2023-12-01 19:41:02 25 4
gpt4 key购买 nike

我最近开始学习java中的继承,但我在这里遇到了一点问题,我可以在子类的构造函数上放置一个实际的单词作为字符串(而不是变量)吗?

我尝试将其设置为 public Profesori(String emri, int vitiLindjes, "Ligjerate", String thirrjaAkademike) 但没有运气

public class Mesimdhenesi {
private String emri;
private int vitiLindjes;
private String angazhimi;

public Mesimdhenesi(String emri, int vitiLindjes, String angazhimi) {

this.emri = emri;
this.vitiLindjes = vitiLindjes;
this.angazhimi = angazhimi;
}
}

public class Profesori extends Mesimdhenesi {
private String thirrjaAkademike;


public Profesori(String emri, int vitiLindjes, "Ligjerate", String thirrjaAkademike) {

super(emri,vitiLindjes);

this.thirrjaAkademike = thirrjaAkademike;

}
}

出现此错误:Profesori.java:6:错误:类型的非法开始 public Profesori(String emri, int vitiLindjes, "Ligjerate", String thirrjaAkademike) {

最佳答案

不能在方法或构造函数的定义中使用字符串文字。
如果您想将字符串“Ligjerate” 作为 super() 构造函数的参数传递,您的 Profesori 构造函数应类似于:

public Profesori(String emri, int vitiLindjes, String thirrjaAkademike) {

super(emri,vitiLindjes, "Ligjerate");

this.thirrjaAkademike = thirrjaAkademike;

}

关于java - 传递一个字符串作为构造函数参数并获取 "illegal start of type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55393182/

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