Java public class Employee {String name; int salary; public 【代码1】{ //构造方法 this.name=name; this.salary=salary; } public String getDetails() { return " Name: " + name + "," + "Salary: " + salary; } } public class Manager extends Employee { //类继承;主类Manager包含main()方法 String department; //新增属性(字段) public Manager(String name,int salary,String department){ 【代码2】 //调用父类的构造方法 this.department=department; } public String getDetails() { return +name "salary," + "部门:" + department; //在子类里调用父类的构造函数 } public static void main(String[] args) { Manager m = new Manager("张三",5000,"财务部"); System.out.println(m.getDetails()); } }
时间: 2023-05-21 16:06:44 浏览: 75
代码1应该是public Employee(String name,int salary),其中参数name和salary分别表示员工的姓名和薪水。
代码2应该是super(name,salary),用于调用父类Employee的构造方法,以便完成员工姓名和薪水的初始化。
相关问题
解释这些代码import java.util.TreeSet;public class Employee implements Comparable<Employee> { private int id; private String name; private double salary; public Employee(int id, String name, double salary) { this.id = id; this.name = name; this.salary = salary; } public int getId() { return id; } public String getName() { return name; } public double getSalary() { return salary; } @Override public int compareTo(Employee employee) { return Integer.compare(this.id, employee.id); } @Override public String toString() { return "Employee{" + "id=" + id + ", name='" + name + '\'' + ", salary=" + salary + '}'; } public static void main(String[] args) { TreeSet<Employee> employees = new TreeSet<>(); employees.add(new Employee(2, "Tom", 5000)); employees.add(new Employee(1, "Jerry", 6000)); employees.add(new Employee(3, "Bob", 4000)); System.out.println(employees); }}
这段代码定义了一个名为 Employee 的类,该类实现了 Comparable 接口,并重写了 compareTo 方法,以便在 TreeSet 中进行比较和排序。Employee 类有三个属性:id、name 和 salary,以及一个构造函数和三个 getter 方法。在 main 方法中,使用 TreeSet 对象 employees 存储了三个 Employee 对象,并打印输出了它们。由于 Employee 实现了 Comparable 接口,TreeSet 可以按照 id 属性值的大小将 Employee 对象进行排序,并输出排序后的结果。输出结果为:[Employee{id=1, name='Jerry', salary=6000.0}, Employee{id=2, name='Tom', salary=5000.0}, Employee{id=3, name='Bob', salary=4000.0}]。
package com.unit2.test; public class Test4 { public static void main(String[] args) { Salary s=new Salary("员工A","北京",3,3600.00); Employee e=new Salary("员工B","上海",2,2400.00); System.out.println("使用Salary的引用调用mailcheck--"); s.mailCheck(); System.out.println("\n使用employee的引用调用mailcheck--"); e.mailCheck(); } public class Employee{ private String name; private String address; private int number; public Employee(String name,String address,int number) { System.out.println("Employee 构造函数"); this.name=name; this.address=address; this.number=number; } public void mailCheck() { System.out.println("邮寄支票给:"+this.name+" "+this.address); } public String toString() { return name+" "+address+" "+number; } public String getName() { return name; } public String getAddress() { return address; } public void setAddress(String newAddress) { address=newAddress; } public int getNumber() { return number; } } public class Salary extends Employee { private double salary;//全年工资 public Salary(String name,String address,int number,double salary) { super(name,address,number); setSalary(salary); } public void mailCheck() { System.out.println("Salary类的mailcheck方法"); System.out.println("邮寄支票给:"+getName()+",工资为"+salary); } public double getSalary() { return salary; } public void setSalary(double newSalary) { if(newSalary>=0.0) { salary=newSalary; } } public double computePay() { System.out.println("计算工资,付给:"+getName()); return salary/52; } } }更正代码错误
根据代码格式,你应该是在一个文件中定义了两个类Test4和Employee以及一个子类Salary,但是在Test4类中定义了Employee类,这是不正确的。应该将Employee类定义在Test4类的外部,例如:
```
package com.unit2.test;
public class Test4 {
public static void main(String[] args) {
Salary s = new Salary("员工A", "北京", 3, 3600.00);
Employee e = new Salary("员工B", "上海", 2, 2400.00);
System.out.println("使用Salary的引用调用mailcheck--");
s.mailCheck();
System.out.println("\n使用employee的引用调用mailcheck--");
e.mailCheck();
}
}
class Employee {
private String name;
private String address;
private int number;
public Employee(String name, String address, int number) {
System.out.println("Employee 构造函数");
this.name = name;
this.address = address;
this.number = number;
}
public void mailCheck() {
System.out.println("邮寄支票给:" + this.name + " " + this.address);
}
public String toString() {
return name + " " + address + " " + number;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public void setAddress(String newAddress) {
address = newAddress;
}
public int getNumber() {
return number;
}
}
class Salary extends Employee {
private double salary;// 全年工资
public Salary(String name, String address, int number, double salary) {
super(name, address, number);
setSalary(salary);
}
public void mailCheck() {
System.out.println("Salary类的mailcheck方法");
System.out.println("邮寄支票给:" + getName() + ",工资为" + salary);
}
public double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
if (newSalary >= 0.0) {
salary = newSalary;
}
}
public double computePay() {
System.out.println("计算工资,付给:" + getName());
return salary / 52;
}
}
```
阅读全文