Eclipse 说 The constructor Email() is undefined

Eclipse says The constructor Email() is undefined

我正在尝试扩展我正在关注的教程 Java 项目逐步构建电子邮件管理应用程序 (https://www.youtube.com/watch?v=U3Ibvu0htNs&t=386s)。我正在尝试使用 getter 和 setter 来允许封装完成它的工作,并最终允许用户输入他们自己的名称,而不是在变量中使用预设名称。我知道代码很乱我玩这段代码的时间比我想承认的要长。 XD

package encap;

public class Main {

    public static void main(String[] args) {
        Email s = new Email();
        
        s.setName("Billy");
        
        System.out.println(s.getName());
        
        
        //Email em1 = new Email("John", "Smith");
        //System.out.println (em1.ShowInfo());
    }

}
package encap;

import java.util.Scanner;

public class Email {
    private String firstName;
    private String lastName;
    private String password;
    private String department;
    private String email;
    private int mailboxCapacity = 500;
    private String alternateEmail;
    private int passwordDefaultLength = 10;
    private String companySuffix = "company.com";
    
    //Constructor to receive first and lastName
    public Email() {
        this.firstName = firstName;
        this.lastName = lastName;
        
        //call a method asking for the department return the department
        this.department = setDepartment();
        
        // Call a method that returns a random password
        this.password = randomPassword(passwordDefaultLength);
        System.out.println("Your password is: " + password);
        
        //combine elements to generate email
        email = firstName.toLowerCase() + "." + lastName.toLowerCase() + "@" + department.toLowerCase() + "." + companySuffix.toLowerCase();
    }

    // Ask for the department
    private String setDepartment() {
        System.out.print("New worker: " + firstName + " " + lastName + "\nDEPARTMENT CODES:\n1 for Sales\n2 for Development\n3 for Accounting \n0 for none\nEnter 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 ""; }
        }
    
    //Generate random password
    private String randomPassword(int length) {
        String passwordSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()";
        char[] password = new char[length];
        for(int i=0; i<length; i++) {
            int rand = (int) (Math.random() * passwordSet.length());
            password[i] = passwordSet.charAt(rand);
        }
        return new String(password);
    }
    
    //Employee First and Last name getter and setter

///////trying this

    
    public void setName(String newName) {
        firstName = newName;
    }
    
    public String getName() {
        return firstName;
    }

    
    
    // setters & getters mailbox capacity
    public void setMailboxCapacity(int capacity) {
        this.mailboxCapacity = capacity;
    }
        public int getMailboxCapacity() {
        return mailboxCapacity;
    }
    //setter and getter  alternate email
    

    public String getAlternateEmail() {
        return alternateEmail;
    }
    public void setAlternateEmail(String altEmail) {
        this.alternateEmail = altEmail;
    }
    
    
    public String getPassword() {
        return password;
    }
    
    //change the password
    public void ChangePassword(String password) {
        this.password = password;
    }
    
    public String ShowInfo() {
        return "Display Name: " + firstName + " " + lastName +
            "\nCompany Email: " + email +
            "\nMailbox Capacity: " + mailboxCapacity + "mb.";
        
    }

}

a) "Email s = new Email();" 这一行应该工作正常。只需确保您是否在 运行 it.But 之前保存了程序,您将在

处获得空指针异常

电子邮件 = firstName.toLowerCase() + "." + lastName.toLowerCase() + "@" + department.toLowerCase() + "." + companySuffix.toLowerCase();

b) 您提到的错误只有在您取消注释“Email em1 = new Email("John", "Smith");" 行时才会发生并执行程序,因为 class 电子邮件中没有参数化构造函数。 那时您可能会收到错误消息“构造函数 Email(String, String) 未定义”。

但是当前的构造函数实例化应该可以工作fine.Just ensure(a)