employee1上的方法声明无效需要返回类型

时间:2014-10-03 01:22:21

标签: java

我在公共静态Employee1上收到错误,说它需要一个返回类型,并且方法声明无效。我试过改变它但我不知道我做错了什么。对不起,我对java有点新鲜了

import java.util.Scanner;

public class aleko_Employee1 extends Object
{
private String firstName;
private String lastName;
private char middleInitial;
private boolean fulltime;
private char gender;
private int employeeNum;
private static Scanner in = new Scanner(System.in);

public class aleko_Employee1
{

public static void main(String args[])
{
    aleko_Employee1 employee1 = new aleko_Employee1("Jeff", "Doe", 'M', 12345);
    aleko_Employee1 employee2 = new aleko_Employee1("Jeffery", "Doe", 'M', 12345);
    aleko_Employee1 employee3 = new aleko_Employee1("Amanda", "Smith", 'M', 98765);
}
public static Employee1(String fn, String ln, char g,  int en)
{

    firstName = fn;
    lastName = ln;
    gender = g;
    employeNum = en;
}

public void setfirstName(String fn)
{
    firstName = fn;
}

public String getFirstName()
{
    return firstName;
}

public void setLastName(String ln)
{
    lastName=ln;
}

public String getLastName()
{
    return lastName;
}

public void setGender(char g)
{
    gender =g;
}

public char getGender()
{
    return gender;
}

public void setEmployeeNumber(int en)
{

    if ( en > 99999 || en < 10000)
    {
        employeeNum = en;
    }
    else
    {
        employeeNum = 0;
    }
}
public int getEmployeeNumber();
{
    return employeeNum;
}

public boolean equals( Object e2)
{
    if (this.employeeNum == ((Employee1)e2).employeeNum)
    {
        return true;
    }
    else
    {
        return false;
    }
}

public String toString()
{
    return lastName + ","+ "\n" + "ID:" + employeeNum + "\n";
}
}

}

2 个答案:

答案 0 :(得分:0)

更改

public static Employee1(String fn, String ln, char g,  int en)
{
...
}

to(你总是需要在方法中指定一个返回类型,如果它不返回任何内容void

public static void Employee1(String fn, String ln, char g,  int en)
{
...
}

或者它应该是构造函数(您没有在构造函数中指定返回类型)

public aleko_Employee1(String fn, String ln, char g,  int en)
{
...
}

答案 1 :(得分:0)

您可能尝试定义构造函数,但最终在此处定义了一个方法:

public static Employee1(String fn, String ln, char g,  int en)
{

    firstName = fn;
    lastName = ln;
    gender = g;
    employeNum = en;
}

构造函数需要与Class具有相同的名称,并且它们没有任何返回类型。所以改成它:

public aleko_Employee1(String fn, String ln, char g,  int en)
{

    firstName = fn;
    lastName = ln;
    gender = g;
    employeNum = en;
}

使用new运算符创建对象时会调用构造函数。所以现在当你按照这里提到的创建对象时:

aleko_Employee1 employee1 = new aleko_Employee1("Jeff", "Doe", 'M', 12345);
aleko_Employee1 employee2 = new aleko_Employee1("Jeffery", "Doe", 'M', 12345);
aleko_Employee1 employee3 = new aleko_Employee1("Amanda", "Smith", 'M', 98765);

JVM将调用匹配的构造函数,如上所述。