在调用构造函数时遇到问题,如果Java类在不同的路径中。

huangapple go评论102阅读模式
英文:

Problem at calling a constructor if the java class is in a different path

问题

以下是翻译好的内容:

我想将 Java 类保存在一个名为 "Classes" 的文件夹中,在 NetBeans 中,我已经创建了那个文件夹,然后将名为 "Jugadores.java" 的类保存在其中,但在这样做之后,我在主类中调用名为 "regisPlayer" 的构造函数时遇到了问题。
NetBeans 显示:

找不到符号
符号:类 "regisPlayer"

位置:类 "Obligatorio"

这是我的主类:

  1. package obligatorio;
  2. import java.util.*;
  3. import obligatorio.classes.*;
  4. public class Obligatorio {
  5. public static void main(String[] args) {
  6. Jugadores();
  7. }
  8. static void Jugadores() {
  9. Scanner in = new Scanner(System.in);
  10. System.out.println("玩家姓名");
  11. String Name = in.nextLine();
  12. System.out.println("玩家年龄");
  13. int Edad = in.nextInt();
  14. Jugadores player = new regisPlayer(Name, Edad); // 在此行显示找不到符号 "regisPlayer"
  15. }
  16. }

这是我的 "Jugadores.java" 类:

  1. package obligatorio.classes;
  2. public class Jugadores {
  3. private String nombre;
  4. private int edad;
  5. public void regisPlayer(String Nombre, int Edad) {
  6. this.nombre(Nombre);
  7. this.edad(Edad);
  8. }
  9. public void nombre(String Nombre) {
  10. nombre = Nombre;
  11. }
  12. public void edad(int Edad) {
  13. edad = Edad;
  14. }
  15. }

我不知道问题可能是什么,我正在学习 Java。我已经尝试通过在 "Jugadores.java" 中添加包名 "obligatorio.classes" 来解决问题,然后在主类中调用它,但没有成功。

英文:

I want to save the java classes in a folder called Classes, so on NetBeans, I have created that folder, and then I have saved it the class called Jugadores.java but after doing that i am having issues to call my constructor called regisPlayer on the main class.
NetBeans says that:

cannot find the simbol
symbol:class regisPlayer

location: class Obligatorio

This is my main class

  1. package obligatorio;
  2. import java.util.*;
  3. import obligatorio.classes.*;
  4. public class Obligatorio {
  5. public static void main(String[] args) {
  6. Jugadores();
  7. }
  8. static void Jugadores() {
  9. Scanner in = new Scanner(System.in);
  10. System.out.println("Player Name ");
  11. String Name = in.nextLine();
  12. System.out.println("Age Player ");
  13. int Edad = in.nextInt();
  14. Jugadores player = new regisPlayer(Name, Edad); // On this line says than can not find the symbol regisPlayer
  15. }
  16. }

This is my class Jugadores.java

  1. package obligatorio.classes;
  2. public class Jugadores {
  3. private String nombre;
  4. private int edad;
  5. public void regisPlayer(String Nombre, int Edad) {
  6. this.nombre(Nombre);
  7. this.edad(Edad);
  8. }
  9. public void nombre(String Nombre) {
  10. nombre = Nombre;
  11. }
  12. public void edad(int Edad) {
  13. edad = Edad;
  14. }
  15. }

I do not what it could be the problem, I am learning Java. I have tried to solve the problem adding the name package obligatorio.classes in Jugadores.java to then call it on my main class but I did not work.

答案1

得分: 0

  1. package obligatorio.classes;
  2. public class Jugadores {
  3. private String nombre;
  4. private int edad;
  5. public Jugadores(String nombre, int edad) {
  6. this.nombre = nombre;
  7. this.edad = edad;
  8. }
  9. public void setNombre(String Nombre) {
  10. nombre = Nombre;
  11. }
  12. public void setEdata(int Edad) {
  13. edad = Edad;
  14. }
  15. }
  1. Jugadores player = new Jugadores("name example", "edad example");
英文:
  1. package obligatorio.classes;
  2. public class Jugadores {
  3. private String nombre;
  4. private int edad;
  5. public Jugadores(String nombre, int edad) { //this is a constructor
  6. this.nombre = nombre;
  7. this.edad = edad;
  8. }
  9. public void setNombre(String Nombre) { //only need setters if you plan to change it
  10. nombre = Nombre;
  11. }
  12. public void setEdata(int Edad) {//only need setters if you plan to change it
  13. edad = Edad;
  14. }
  15. }

Used like this :

  1. Jugadores player = new Jugadores("name example", "edad example");

huangapple
  • 本文由 发表于 2020年10月3日 01:48:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64176136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定