I understand most of the code below.
I just don't understand the purpose of the no-arg constructors of Employee and Person since I get the same results when I delete them. I am new to java so I apologize if this is a dumb question. This is a code snippet for one of my classes.
Any help will be appreciated.
public class TestEmployee {
public static void main(String[] args) {
// Define some employees
Employee president = new Employee("Lucy", "President", 100000);
System.out.println(president);
Employee cto = new Employee("Vincent", "Chief Tech Officer", 70000);
System.out.println(cto);
}
}
class Employee extends Person{
// ADD YOUR CODE HERE!!!
// Nothing above needs to change.
private String jobTitle="Unknown";
private int salary = 0;
public Employee() {
}
public Employee(String name, String jobTitle, int salary) {
super(name);
this.jobTitle = jobTitle;
this.salary = salary;
}
@Override
public String toString() {
return this.getName() + " is the " + jobTitle + " and makes $" + salary + " a year.
";
}
// You need data fields, a constructor or two, and a to-string method
}
// IF YOU ALREADY HAVE THE PERSON CLASS IN YOUR WORKSPACE
// YOU CAN DELETE THIS. OTHERWISE YOU'LL GET AN ERROR:
// 'The type Person is already defined.'
class Person {
private String name = "Default Name";
// No-arg Constructor
public Person () {
}
public Person (String name){
this.name = name;
}
// Getter method
public String getName() {
return name;
}
// Setter method
public void setName(String newName) {
if (!newName.equals("")) {
name = newName;
}
else
System.out.println("Can't change name. Empty names aren't allowed!");
}
public String toString() {
return "Name: " + name + "
";
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…