我在定义构造函数类时遇到麻烦

时间:2019-08-12 04:41:54

标签: java eclipse

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

主类(电子邮件)

package EmailApp;

public class Email {
    public static void main(String[] args) {
        EmailApp Email1 = new EmailApp();
        Email1.setFullName();
    }
}

公共类(EmailApp)

package EmailApp;

import java.util.Scanner;

public class EmailApp {
    String firstName;
    String lastName;
    String password;
    String department;
    int mailboxCapacity;
    int defaultPasswordLength = 10;
    String alternateEmail;
    //Declaration of all objects

    public EmailApp(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;

        this.department = setDepartment();
        System.out.println("Department: " + this.department);
        // Printing the Department Name

        this.password = randomPassword(defaultPasswordLength);
        System.out.println("Your Password Is: " + this.password);
        //Printing Password generation results

        this.firstName = setFullName();
        this.lastName = setFullName();

    }
    //Entering the First and Last Name for Email Creation here

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

    private String setDepartment() {
        System.out
                .print("Department Codes\n1 for Sales\n2 for Development\n3 for Accounting\n0 for None\nEnter the Department Code:");
        Scanner in = new Scanner(System.in);
        int depChoice = in.nextInt();
        if (depChoice == 1) {
            return "Sales";
        } else if (depChoice == 2) {
            return "Development";
        } else if (depChoice == 3) {
            return "Accounting";
        } else {
            return " ";
        }
        //Setting parameters for department codes and scanner for input of code
    }

    private String randomPassword(int length) {
        String passwordSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*";
        char[] password = new char[length];
        for (int i = 0; i < length; i++) {
            int rand = (int) (Math.random() * passwordSet.length());
            password[i] = passwordSet.charAt(rand);
            //Password Generation
        }
        return new String(password);
    }
}

5 个答案:

答案 0 :(得分:2)

  

EmailApp Email1 =新的EmailApp();

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

EmailApp Email1 = new EmailApp("John", "Doe");

或创建一个非参数构造函数:

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

另外,看看Why do we need private variables?

答案 1 :(得分:1)

出现此错误是因为在EmailApp类中有一个参数化的构造函数。  公共EmailApp(字符串firstName,字符串lastName)

您正在尝试访问默认构造函数。这就是为什么会出现此错误。

您需要在EmailApp类中声明如下的默认构造函数,并且不会给出错误消息:

public EmailApp() {

    }

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

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

答案 2 :(得分:0)

无论何时创建任何Java类,该特定类的构造函数都将可用,因为构造将由编译器本身完成。

但是,每当您定义自定义构造函数时,默认构造函数将对您不可用...

例如,您上课

您定义的班级:

公共类EmailApp { }

编译后:

公共类EmailApp {

公共EmailAp(){ }

}

因此,默认的构造函数将由编译器创建。

但是您正在定义自己的构造函数,因此编译器无需创建默认构造函数。

请记住,对于Java中的任何类,始终必须有一个构造函数。

答案 3 :(得分:0)

您是EmailApp的构造函数,在构造EmailApp并传递0个参数时,它使用两个参数(字符串firstName,字符串lastName)。

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

答案 4 :(得分:0)

不使用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。