【问题标题】:Eclipse says The constructor Email() is undefinedEclipse 说构造函数 Email() 未定义
【发布时间】:2021-10-28 22:17:30
【问题描述】:

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

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.";
        
    }

}

【问题讨论】:

  • 您是否在菜单栏中的“项目”菜单上启用了自动构建?您的代码完全“构建”了吗?
  • 也许Project/clean 有帮助;看起来您的 播放 导致您的 Email 类的最新构建版本与源代码不同步。

标签: java eclipse getter-setter encapsulation


【解决方案1】:

a) 行“Email s = new Email();”应该可以正常工作。只需确保您在运行程序之前是否已保存程序。但是您会在

处得到一个空指针异常

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

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

但是当前的构造函数实例化应该可以正常工作。只需确保(a)

【讨论】:

  • 非常感谢,我删除了编辑过的“Email em1 = new Email("John", "Smith");"说 'em1.setFirstName("Bo"); em1.setLastName("哈里斯"); System.out.println(em1.getFirstName());'现在它只是在我运行程序时说 Null Null 所以它不再出现错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-11
  • 2020-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多