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

主班(电子邮件)

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

最佳答案

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?

09-09 22:58