简体   繁体   English

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

[英]I am having trouble defining my constructor class

I am trying to call a method from another class into my main class, and it is telling me that the constructor I am using is undefined. 我试图从另一个类调用一个方法到我的主类,它告诉我我正在使用的构造函数是未定义的。 Any suggestions for fixing? 有任何修复建议吗? possibly what else I am doing wrong? 可能还有什么我做错了?

Main Class (Email) 主类(电子邮件)

package EmailApp;

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

Public Class(EmailApp) 公共类(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 = new EmailApp(); EmailApp Email1 = new EmailApp();

Your constructor is public EmailApp(String firstName, String lastName) . 您的构造函数是public EmailApp(String firstName, String lastName) As it is obvious, you do not pass any arguments to it. 很明显,你不会传递任何参数。 Its usage should be something like: 它的用法应该是这样的:

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

or create a non argument constructor: 或者创建一个非参数构造函数:

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

Also, take a look at Why do we need private variables? 另外,看看为什么我们需要私有变量?

It is giving this error because you have one parameterized constructor in EmailApp class. 它给出了这个错误,因为您在EmailApp类中有一个参数化构造函数。 public EmailApp(String firstName, String lastName) public EmailApp(String firstName,String lastName)

And yout are trying to access default constructor. 而且你正在尝试访问默认构造函数。 that's why it is giving this error. 这就是它给出这个错误的原因。

You need to declare default constructor like below in the EmailApp class and it will not give the Error: 您需要在EmailApp类中声明如下所示的默认构造函数,它不会给出错误:

public EmailApp() {

    }

OR use the parameterized constructor you have declared in the class like : 或者使用您在类中声明的参数化构造函数,如:

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

Whenever we created any java class, constructor for that particular class will already available, as the construction will be done by compiler itself. 每当我们创建任何java类时,该特定类的构造函数就已经可用,因为构造将由编译器本身完成。

But whenever you define your custom constructor, at that time default constructor won't be available for you... 但是无论何时定义自定义构造函数,那时默认构造函数都不可用...

For example you have class 例如,你有课

Your defined class : 你定义的类:

Public class EmailApp { } 公共类EmailApp {}

After compilation: 编译后:

public class EmailApp { 公共类EmailApp {

public EmailAp(){ } public EmailAp(){}

} }

So default constructor will be created by compiler. 因此默认构造函数将由编译器创建。

But you are defining your own constructor so compiler don't need to create default one. 但是您正在定义自己的构造函数,因此编译器不需要创建默认构造函数。

Remember always one constructor must require for any class in java. 请记住,java中的任何类都必须有一个构造函数。

You're EmailApp 's constructor takes two parameters (String firstName, String lastName), while you are constructing EmailApp and passing 0 parameters. 你正在构建EmailApp并传递0个参数,你的EmailApp的构造函数需要两个参数(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();
    }
}

Doesnt the setfullName Method crush your code as it runs itself the whole time 没有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.
}

You should either let the method return something else or let it return void. 你应该让方法返回别的东西或让它返回void。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM