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; 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; } } } 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-07 12:02:53 浏览: 249
C#中static void Main(string[] args) 参数示例详解
在以上代码中,需要做出如下更正:
1. 将Test4类中的两个内部类Employee和Salary移出Test4类,并分别保存为Employee.java和Salary.java文件。
2. 修改Employee和Salary的构造函数,使得它们的第三个参数number改为protected类型,以便在Salary类中访问。
3. 在Test4类中,将Employee e=new Salary("员工B","上海",2,2400.00); 改为Employee e=new Employee("员工B","上海",2); 以避免类型不匹配错误。
下面是更正后的代码:
Employee.java
```
package com.unit2.test;
public class Employee {
private String name;
private String address;
protected 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;
}
}
```
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 Employee("员工B","上海",2);
System.out.println("使用Salary的引用调用mailcheck--");
s.mailCheck();
System.out.println("\n使用employee的引用调用mailcheck--");
e.mailCheck();
}
}
```
阅读全文