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; } } }更正以上代码错误
时间: 2024-01-06 20:05:31 浏览: 83
在以上代码中存在两个问题:
1. Test4类中定义了两个public类。Java中一个源文件中只能有一个public类,因此需要将Employee类和Salary类单独定义在不同的源文件中。
2. 在Employee类中没有定义getSalary()和setSalary()方法,而这两个方法在Salary类中被使用了,因此需要在Employee类中添加这两个方法。
以下是修改后的代码:
Employee.java
```
package com.unit2.test;
public class Employee{
private String name;
private String address;
private int number;
private double salary;
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 double getSalary() {
return salary;
}
public void setSalary(double newSalary) {
if (newSalary >= 0.0) {
salary = newSalary;
}
}
}
```
Salary.java
```
package com.unit2.test;
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.java
```
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();
}
}
```
阅读全文