使用对象和访问器方法

时间:2014-03-24 22:35:01

标签: java object

好吧,所以我创建了一个单独的类,它有访问器和mutator方法,构造函数等等。它编译所有没问题。

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

继承错误:

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

}

2 个答案:

答案 0 :(得分: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.");

答案 1 :(得分:0)

你错过了' +'在emp1.getPosition()之前的print语句中的运算符。