2014-03-24 171 views
-1

好吧,所以我创建了一个具有访问器和增变器方法,构造函数等的独立类。它编译所有的罚款没问题。使用对象和访问器方法

现在我有了这个演示类,它应该使用来自其他类的信息来产生结果。很简单,但是我收到了一些错误。我认为这个问题可能很简单,但我无法弄清楚。

继承人的错误:

EmployeeDemoWilson.java:19: error: ')' expected 
System.out.println("Employee ID Number " + emp1.getIdNumber() + " is " emp1.getName() + ". Susan is the " emp1.getPosition() 
                    ^
EmployeeDemoWilson.java:19: error: illegal start of expression 
System.out.println("Employee ID Number " + emp1.getIdNumber() + " is " emp1.getName() + ". Susan is the " emp1.getPosition() 
                     ^
EmployeeDemoWilson.java:19: error: ';' expected 
System.out.println("Employee ID Number " + emp1.getIdNumber() + " is " emp1.getName() + ". Susan is the " emp1.getPosition() 
                       ^
EmployeeDemoWilson.java:20: error: not a statement 
        + " of the " + emp1.getDepartment() + " department."); 
                ^
EmployeeDemoWilson.java:20: error: ';' expected 
        + " of the " + emp1.getDepartment() + " department."); 
                    ^
5 errors 

继承人的代码中的错误是来自:

public class EmployeeDemo 
{ 

public static void main (String [] args) 
{ 

// Creating the employee objects. 
Employee emp1 = new Employee("Susan Meyers", 47899, "Accounting", "Vice President"); 
Employee emp2 = new Employee("Mark Jones", 39119, "IT", "Programmer"); 
Employee emp3 = new Employee("Joy Rogers", 81774, "Manufacturing", "Engineer"); 

//Displayng the employee information. 

System.out.println("Employee ID Number " + emp1.getIdNumber() + " is " emp1.getName() + ". Susan is the " emp1.getPosition() 
        + " of the " + emp1.getDepartment() + " department."); 

    } 
} 

继承人具有构造函数,方法还等什么等原班。它编译好。

public class Employee 
{ 
// A reference to a string object that holds the employee's name 
private String name; 

// An int variable that holds the employee's ID number 
private int idNumber; 

// A reference to a string object that holds the department in which the employee works. 
private String department; 

// A reference to a string object that holds the job title of the employee. 
private String position; 

/** Constructor 
    @param name The name of the employee. 
    @param idNumber The ID number of the employee. 
    @param department The department the employee belongs to. 
    @param position The job title of the employee. 
*/ 
public Employee(String name, int idNumber, String department, String position) 
{ 
    this.name = name; 
    this.idNumber = idNumber; 
    this.department = department; 
    this.position = position; 
} 

/** Constructor 2 
    @param name The name of the employee. 
    @param idNumber The ID number of the employee. 
*/ 

public Employee(String name, int idNumber) 
{ 
    this.name = name; 
    this.idNumber = idNumber; 
    department = ""; 
    position = ""; 
} 

/** Constructor 3 
*/ 
public Employee() 
{ 
    name = ""; 
    idNumber = 0; 
    department = ""; 
    position = ""; 
} 

/** 
    Sets the name field. 
    @param empn The employee's name. 
*/ 
public String setName(String empn) 
{ 
    return name = empn; 
} 

/** 
    Sets the idNumber field. 
    @param idn The employee's ID number. 
*/ 
public int setIdNumber(int idn) 
{ 
    return idNumber = idn; 
} 

/** 
    Sets the department field. 
    @param dept The department the employee belongs to. 
*/ 
public String setDepartment(String dept) 
{ 
    return department = dept; 
} 

/** 
    Sets the position field. 
    @param pos The job title the employee holds. 
*/ 
public String setPosition(String pos) 
{ 
    return position = pos; 
} 

/** 
    Gets the name of the employee. 
    @return The employee's name. 
*/ 
public String getName() 
{ 
    return name; 
} 

/** 
    Gets the id number of the employee. 
    @return The employee's ID number. 
*/ 
public int getIdNumber() 
{ 
    return idNumber; 
} 

/** 
    Gets the department the employee belongs to. 
    @return The department the employee belongs to. 
*/ 
public String getDepartment() 
{ 
    return department; 
} 

/** 
    Gets the position the employee has. 
    @return The job title the employee holds. 
*/ 
public String getPosition() 
{ 
    return position; 
} 

} 

回答

1

你错过了你的连接字符串的几个+连接操作。更改

System.out.println("Employee ID Number " + emp1.getIdNumber() + " is " emp1.getName() 
    + ". Susan is the " emp1.getPosition() 
    + " of the " + emp1.getDepartment() + " department."); 

//                 Here 
System.out.println("Employee ID Number " + emp1.getIdNumber() + " is " + emp1.getName() 
//     and here 
    + ". Susan is the " + emp1.getPosition() 
    + " of the " + emp1.getDepartment() + " department."); 
+0

这不就是一个耳光。我必须走开,而不是放他们。 – WillBro

0

你已经错过了 '+' 在打印语句只是emp1.getPosition之前运营商()。