package test; public class pp { private String name=""; private String addreass=""; public pp(String name2, String address) { // TODO Auto-generated constructor stub } public void pp (String name , String addreass) { this.name=name; this.addreass=addreass; } } public abstract class Employee extends pp{ private String ID=""; private double wage=0; private int age =0; public Employee(String name, String address, String ID, double wage, int age) { super(name, address); this.ID = ID; this.wage = wage; this.age = age; } public abstract void add (String position); } 帮我改错
时间: 2023-11-22 08:54:42 浏览: 136
在pp类中,您需要将构造函数的参数名称与类变量名称匹配。修改后的代码如下:
```
package test;
public class pp {
private String name = "";
private String address = "";
public pp(String name, String address) {
this.name = name;
this.address = address;
}
}
```
在Employee类中,您需要添加一个具体的构造函数来调用父类的构造函数。修改后的代码如下:
```
package test;
public abstract class Employee extends pp {
private String ID = "";
private double wage = 0;
private int age = 0;
public Employee(String name, String address, String ID, double wage, int age) {
super(name, address);
this.ID = ID;
this.wage = wage;
this.age = age;
}
public abstract void add(String position);
}
```
相关问题
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(String newAddress) { return address; } public void setAddress(String newAddress) { address=newAddress; } public int getNumber() { return number; } } public static 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; } } }更正以上代码错误
以下是更正后的代码:
```
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 static 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 static 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;
}
}
}
```
更正说明:
1. 将 `Employee` 类和 `Salary` 类都声明为静态内部类,以便在 `main` 方法中使用。
2. `Employee` 类中的 `getAddress` 方法声明错误,应该是 `public String getAddress()`。
3. 在 `Salary` 类中重写了 `mailCheck` 方法,输出的信息更具体,同时也调用了 `getName` 方法来获取员工名字。
4. 在 `Salary` 类中新增了 `computePay` 方法,用于计算每周的工资。
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;
}
}
```
阅读全文