Java打印对象地址?尝试过name.toString和Array.toString(name)

时间:2016-10-02 22:58:30

标签: java arrays object

我差不多完成了这项任务,但是我的员工阵列" emp"尽管我已经尝试过更改,但仍在打印对象地址。你能提供一些指导吗?根据老师的说法,我不能编辑主课程。谢谢!

当前代码:

import java.util.Arrays;
public class Manager extends Employee
{
int index = -1;
Employee[] emp;

public Manager(int size){
    emp = new Employee[size];
}

void addEmployee(Employee e){
    index++;
    emp[index] = e;
}   
public void printRecord(){
    super.printRecord();
    for (int i = 0; i < emp.length; i++)
        System.out.println("Employees being managed: " + emp[i].toString());
}

}

以下是员工类,如果相关:

public class Employee
{
private String name;
private int id;
private int age;
private double salary;
private String title;
private String department;


void printRecord(){
    System.out.println("\n\n\nEmployee Name : " + getName());
    System.out.println("Employee Id : " + this.getID());
    System.out.println("Employee Age : " + this.getAge());
    System.out.println("Employee salary : " + this.getSalary());
    System.out.println("Employee Title : " + this.getTitle());
    System.out.println("Employee Department : " + this.getDepartment());
}

String getName(){
    return this.name;
}

int getID(){
    return this.id;
}   

int getAge(){
    return this.age;
}

double getSalary(){
    return this.salary;
}

String getTitle(){
    return this.title;
}

String getDepartment(){
    return this.department;
}

void setName(String name){
    this.name = name;
}

void setID(int id){
    this.id = id;
}

void setAge(int age){
    this.age = age;
}

void setSalary(int salary){
    this.salary = salary;
}

void setTitle(String title){
    this.title = title;
}

void setDepartment(String department){
    this.department = department;
}

void changeSalary(int increase){
    this.salary = this.salary + increase;
}

void changeSalary(double increase){
    if (increase >= 0.0 && increase <= 1.0) //Accept values between 0.0 and 1.0
        this.salary = this.salary * (increase + 1);
}

}

教授提供的测试课程

import java.util.*;
import java.io.*;
public class TestEmployee
{
    public static void main(String args[]){
        Employee matt = new Employee();
        /*Set matt's attributes. */
        matt.setName("Matt Levan");
        matt.setID(1337);
        matt.setAge(27);
        matt.setSalary(1000000);
        matt.setTitle("Lab Instructor");
        matt.setDepartment("Computer Science");

        /* Test methods. */
        matt.printRecord();
        matt.changeSalary(0.5);
        System.out.println(matt.getSalary());

        /* Constructor argument determines the size of the Employee array. */
        Manager garrett = new Manager(1);

        /* Set garrett's attributes. */
        garrett.setName("Garrett Poppe");
        garrett.setID(1337);
        garrett.setAge(27);
        garrett.setSalary(1000000);
        garrett.setTitle("Instructor");
        garrett.setDepartment("Computer Science");
        garrett.addEmployee(matt);

        /* Test methods. */
        garrett.printRecord(); // Should printGarret's and Matt's records.
        garrett.changeSalary(0.85); // Should multiply current salary by 1.85.
        System.out.println(garrett.getSalary());
    }

}

0 个答案:

没有答案