如何解决如果Java类位于其他路径中,则调用构造函数时会出现问题
我想将Java类保存在一个名为Classes的文件夹中,因此在NetBeans上,我创建了该文件夹,然后又将其保存为一个名为Jugadores.java的类,但是这样做之后,我遇到了调用我的构造函数的问题在主类上称为regisPlayer。 NetBeans表示:
找不到simbol
symbol:class regisPlayer
位置:class Obligatorio
这是我的主班
package obligatorio;
import java.util.*;
import obligatorio.classes.*;
public class Obligatorio {
public static void main(String[] args) {
Jugadores();
}
static void Jugadores() {
Scanner in = new Scanner(System.in);
System.out.println("Player Name ");
String Name = in.nextLine();
System.out.println("Age Player ");
int Edad = in.nextInt();
Jugadores player = new regisPlayer(Name,Edad); // On this line says than can not find the symbol regisPlayer
}
}
这是我的Jugadores.java类
package obligatorio.classes;
public class Jugadores {
private String nombre;
private int edad;
public void regisPlayer(String Nombre,int Edad) {
this.nombre(Nombre);
this.edad(Edad);
}
public void nombre(String Nombre) {
nombre = Nombre;
}
public void edad(int Edad) {
edad = Edad;
}
}
我不是问题所在,我正在学习Java。我试图解决在Jugadores.java中添加名称包obligatorio.classes以便在我的主类上调用它的问题,但是我没有用。
解决方法
package obligatorio.classes;
public class Jugadores {
private String nombre;
private int edad;
public Jugadores(String nombre,int edad) { //this is a constructor
this.nombre = nombre;
this.edad = edad;
}
public void setNombre(String Nombre) { //only need setters if you plan to change it
nombre = Nombre;
}
public void setEdata(int Edad) {//only need setters if you plan to change it
edad = Edad;
}
}
像这样使用:
Jugadores player = new Jugadores("name example","edad example");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。