Java 我在定义构造函数类时遇到问题

Java 我在定义构造函数类时遇到问题,java,eclipse,Java,Eclipse,我试图从另一个类调用一个方法到我的主类中,它告诉我我使用的构造函数是未定义的。有没有修复的建议?可能我还做错了什么 主要课程(电子邮件) 公共类(EmailApp) 打包EmailApp; 导入java.util.Scanner; 公共类EmailApp{ 字符串名; 字符串lastName; 字符串密码; 弦乐部; 内部邮箱容量; int defaultPasswordLength=10; 字符串替换邮件; //所有对象的声明 公共EmailApp(字符串firstName,字符串lastNa

我试图从另一个类调用一个方法到我的主类中,它告诉我我使用的构造函数是未定义的。有没有修复的建议?可能我还做错了什么

主要课程(电子邮件)

公共类(EmailApp)

打包EmailApp;
导入java.util.Scanner;
公共类EmailApp{
字符串名;
字符串lastName;
字符串密码;
弦乐部;
内部邮箱容量;
int defaultPasswordLength=10;
字符串替换邮件;
//所有对象的声明
公共EmailApp(字符串firstName,字符串lastName){
this.firstName=firstName;
this.lastName=lastName;
this.department=setDepartment();
System.out.println(“部门:+本部门”);
//打印部门名称
this.password=randomPassword(defaultPasswordLength);
System.out.println(“您的密码是:“+this.Password”);
//打印密码生成结果
this.firstName=setFullName();
this.lastName=setFullName();
}
//在此处输入电子邮件创建的名字和姓氏
公共字符串setFullName(){
Scanner firstlastscanner=新扫描仪(System.in);
System.out.println(“输入您的名字:”);
this.firstName=firstlastscanner.nextLine();
System.out.println(“输入您的姓氏;”);
this.lastName=firstlastscanner.nextLine();
firstlastscanner.close();
System.out.println(“创建的电子邮件:“+this.firstName+”“+this.lastName”);
//使用扫描仪输入姓名和姓氏
返回setFullName();
}
私人部门(){
系统输出
.print(“部门代码\n1用于销售\n2用于开发\n3用于会计\n0用于无\n输入部门代码:”);
扫描仪输入=新扫描仪(系统输入);
int depthoice=in.nextInt();
if(depChoice==1){
返回“销售”;
}else if(depChoice==2){
回归“发展”;
}else if(depthoice==3){
返回“会计”;
}否则{
返回“”;
}
//设置部门代码和扫描仪输入代码的参数
}
私有字符串随机密码(整数长度){
字符串密码集=“abcdefghijklmnopqrstuvwxyz123456789!@$%^&*”;
字符[]密码=新字符[长度];
for(int i=0;i
EmailApp Email1=新的EmailApp()

您的构造函数是
public-EmailApp(String-firstName,String-lastName)
。很明显,您没有向它传递任何参数。其用法应类似于:

EmailApp Email1=新的EmailApp(“John”、“Doe”)

或创建非参数构造函数:

public EmailApp()
{
    //Do stuff without arguments
}

另外,请看一下它给出的错误,因为EmailApp类中有一个参数化构造函数。 公共EmailApp(字符串firstName,字符串lastName)

和yout正在尝试访问默认构造函数。这就是它给出这个错误的原因

您需要在EmailApp类中声明如下所示的默认构造函数,它不会给出错误:

public EmailApp() {

    }
或者使用您在类中声明的参数化构造函数,如:

 EmailApp email1 = new EmailApp("james","gosling");
 email1.setFullName();

无论何时创建任何java类,该特定类的构造函数都已经可用,因为构造将由编译器自己完成

但是,每当您定义自定义构造函数时,默认构造函数将不适用于您

比如你有课

您定义的类:

公共类EmailApp{ }

汇编后:

公共类EmailApp{

公共电邮{ }

}

所以默认构造函数将由编译器创建

但您正在定义自己的构造函数,所以编译器不需要创建默认构造函数


请记住,java中的任何类都必须有一个构造函数。

您是
EmailApp
,当您构造
EmailApp
并传递0个参数时,它的构造函数接受两个参数(String firstName,String lastName)

public class Email {
    public static void main(String[] args) {
        /* These are just example names */
        EmailApp Email1 = new EmailApp("Billy", "Bob");
        Email1.setFullName();
    }
}

setfullName方法在整个运行过程中不会破坏代码吗

  public String setFullName() {
    Scanner firstlastscanner = new Scanner(System.in);
    System.out.println("Enter your first name: ");
    this.firstName = firstlastscanner.nextLine();
    System.out.println("Enter your last name; ");
    this.lastName = firstlastscanner.nextLine();
    firstlastscanner.close();

    System.out.println("Email Created: " + this.firstName + " " + this.lastName);
    //Entering the first and last name with a scanner
    return setFullName();//runs itself as return so it will run itself all the time.
}

您应该让该方法返回其他内容,或者让它返回void。

我不同意空构造函数,在这种情况下它没有意义。你不会为姓名为空的人设置电子邮件帐户。作为提醒,你会三次调用#setFullName。这里最好使用静态的
consoleLogin
(或类似的)方法,它可以从用户那里获取输入,然后返回创建的
EmailApp
对象。基本上,您可以使用#setFullName中的当前代码来实现这一点,然后将读取的变量传递到构造函数中,如下面的一些答案所示。但不要添加空构造函数,在这种情况下,它并不真正合适。另外,请查看
Builder
设计模式
public class Email {
    public static void main(String[] args) {
        /* These are just example names */
        EmailApp Email1 = new EmailApp("Billy", "Bob");
        Email1.setFullName();
    }
}
  public String setFullName() {
    Scanner firstlastscanner = new Scanner(System.in);
    System.out.println("Enter your first name: ");
    this.firstName = firstlastscanner.nextLine();
    System.out.println("Enter your last name; ");
    this.lastName = firstlastscanner.nextLine();
    firstlastscanner.close();

    System.out.println("Email Created: " + this.firstName + " " + this.lastName);
    //Entering the first and last name with a scanner
    return setFullName();//runs itself as return so it will run itself all the time.
}