2

The only error I have showing up is that the getName() and getSalary() are not found in there class. I am trying to get the information that is entered into the testEmployee class to use use Employee and display the results. I get a Build Successful message but no output.

public abstract class Person {

    // Variables
    private String name;
    private String ssn = null;

    public Person(String name) {
        this.name = name;
    }

    // Pass data to the object Person
    /**
     * 
     * @param ssn
     */
    public void setSsn(String ssn) {
        this.ssn = ssn;
    }

    public String getSsn() {
        return ssn;
    }

    /**
     * 
     * @return
     */
    public abstract String getName();

}

class Employee extends Person {

    // Variables
    private String jobTitle;
    private double salary;
    private String getName;
    private double cost;

    public Employee(String name) {
        super(name);

        salary = 0;
        jobTitle = null;

    }

    // Pass values to the obljects
    // Setters
    public void setJobTitle(String jobTitle) {
        this.jobTitle = jobTitle;
    }

    public void setSalary(double cost) {
        salary = cost;
    }

    // Getters
    @Override
    public String getName() {
        return getName();
    }

    public String getTitle() {
        return getJobTitle();
    }

    public double getSalary() {
        return salary;
    }

    /**
     * @return the jobTitle
     */
    public String getJobTitle() {
        return jobTitle;
    }

    /**
     * @return the getName
     */
    public String getGetName() {
        return getName;
    }

    /**
     * @param getName
     *            the getName to set
     */
    public void setGetName(String getName) {
        this.getName = getName;
    }

    /**
     * @return the cost
     */
    public double getCost() {
        return cost;
    }

    /**
     * @param cost
     *            the cost to set
     */
    public void setCost(double cost) {
        this.cost = cost;
    }
}

public class testEmployee {

    public static void main(String[] args) {
        Employee emp1 = new Employee("John Smith");
        String ssn = "333224444";
        String jobTitle = "Web Designer";
        double cost = 60000;
        System.out.println("Employee " + getName() "\n The current compensation is " + getSalary());
    }
}
4

2 回答 2

7

你需要在你的函数中拥有emp1.getName()andemp1.getSalary()而不是getName()andgetSalary()printlnmain

于 2013-07-19T16:08:23.603 回答
4

调用and方法时使用方法中Employee emp1created的实例:maingetNamegetSalary

System.out.println
  ("Employee " + emp1.getName() + "\n The current compensation is " + emp1.getSalary());

注意后面的附加+运算符getName()

你也有一个StackOverflowError场景,在这里getName反复称自己为无限

@Override
public String getName() {
   return getName();
}

由于namePerson实现此方法的类成员变量

public String getName() {
   return name;
}

这同样适用于getTitlein Employee。代替

public String getTitle() {
   return getJobTitle();
}

public String getTitle() {
   return jobTitle;
}
于 2013-07-19T16:07:27.783 回答