当前位置: 代码迷 >> Java相关 >> 继承问题
  详细解决方案

继承问题

热度:319   发布时间:2006-09-09 12:25:02.0
继承问题

我做的代码有问题可是我却不知道为什么会有错,代码如下:
public abstract class Employee {
String name;
double basic;
String address;
public Employee(String name,double basic,String address){
this.name=name;
this.basic=basic;
this.address=address;
}
public void show(){
System.out.println("姓名:"+name);
System.out.println("地址:"+address);
System.out.println("薪资:"+basic);
}
public abstract void wages();
}



public class Director
extends Employee
{
double transportAllowance;
double houseRentAllowance;
double dearnessAllowance;
double medicalAllowance;
double enterainmentAllowance;
double leave;
double netPay;
double lessPay;
public Director(String name, double basic, String address) {
super(name, basic, address);
}

public void show() {
super.show();
System.out.println("交通补贴:" + transportAllowance);
}

public void wages(double transportAllowance, double leave) {
double houseRentAllowance = basic * 0.2;
double dearnessAllowance = basic * 0.5;
double medicalAllowance = 4500;
double enterainmentAllowance = 5000;
double totalAmount = basic + houseRentAllowance + dearnessAllowance +
medicalAllowance + enterainmentAllowance + transportAllowance;
double lessPay = (leave <= 5) ? (0.25 * basic) : (0.5 * basic);
double netPay = totalAmount - lessPay;
System.out.println("工资总额:" + totalAmount);
System.out.println("净工资:" + netPay);
}

public void wages(double leave) {
double houseRentAllowance = basic * 0.08;
double dearnessAllowance = basic * 0.3;
double medicalAllowance = 1500;
double totalAmount = basic + houseRentAllowance + dearnessAllowance +
medicalAllowance;
double lessPay = (leave <= 5) ? (0.25 * basic) : (0.5 * basic);
double netPay = totalAmount - lessPay;
System.out.println("工资总额:" + totalAmount);
System.out.println("净工资:" + netPay);
}
}

public class Manager
extends Employee
{
String department;
public Manager(String name,double basic,String address,String department){
super(name,basic,address);
this.department=department;
}
public void show(){
super.show();
System.out.println("部门:"+department);
}
public void wages() {
}
}

这段代码在红字处的错误是这个类不是抽象类不能被重写;蓝字处的错误是找不到这个符号!在黄色的地方提示的是这个错误:"Manager.java": wages() in untitled4.Manager cannot override wages() in untitled4.Employee; attempting to use incompatible return type; found : void, required: double at line 14, column 3
"Manager.java": cannot find symbol; symbol : method show(), location: class untitled4.Employee at line 11, column 11
"Manager.java": wages() in untitled4.Manager cannot override wages() in untitled4.Employee; attempting to use incompatible return type; found : void, required: double at line 14, column 3
还有一个我不明白的是在Director这个类中我在第一个wages方法中加入double leave后他就没有出现Director类不是抽象类不能被重写这个错误,而在第2个wages方法中加入double leave他就提示Director类不是抽象类不能被重写这个错误,这个是为什么啊??

搜索更多相关的解决方案: 继承  

----------------解决方案--------------------------------------------------------
至少要有个和抽象方法一样的签名 public void wages(){}

----------------解决方案--------------------------------------------------------
我在类中不是写了 public void wages(double transportAllowance, double leave){} 的吗?我是要在Director中重写父类中的wages方法! 如果不能这么写的话,那要怎么写啊?还有就是为什么我继承了父类后,用父类的这个show方法可是他却提示找不到这个符号啊?谢谢大大啊!

[此贴子已经被作者于2006-9-9 12:46:56编辑过]



----------------解决方案--------------------------------------------------------
在3个类都去掉public
Director类中写上public void wages(){}

----------------解决方案--------------------------------------------------------

无效!


----------------解决方案--------------------------------------------------------

我在我这通过编译了


----------------解决方案--------------------------------------------------------

abstract class Employee {
String name;
double basic;
String address;
public Employee(String name,double basic,String address){
this.name=name;
this.basic=basic;
this.address=address;
}
public void show(){
System.out.println("姓名:"+name);
System.out.println("地址:"+address);
System.out.println("薪资:"+basic);
}
public abstract void wages();
}

class Director
extends Employee {
double transportAllowance;
double houseRentAllowance;
double dearnessAllowance;
double medicalAllowance;
double enterainmentAllowance;
double leave;
double netPay;
double lessPay;
public Director(String name, double basic, String address) {
super(name, basic, address);
}

public void show() {
super.show();
System.out.println("交通补贴:" + transportAllowance);
}


public void wages(){}
public void wages(double transportAllowance, double leave) {
double houseRentAllowance = basic * 0.2;
double dearnessAllowance = basic * 0.5;
double medicalAllowance = 4500;
double enterainmentAllowance = 5000;
double totalAmount = basic + houseRentAllowance + dearnessAllowance +
medicalAllowance + enterainmentAllowance + transportAllowance;
double lessPay = (leave <= 5) ? (0.25 * basic) : (0.5 * basic);
double netPay = totalAmount - lessPay;
System.out.println("工资总额:" + totalAmount);
System.out.println("净工资:" + netPay);
}

public void wages(double leave) {
double houseRentAllowance = basic * 0.08;
double dearnessAllowance = basic * 0.3;
double medicalAllowance = 1500;
double totalAmount = basic + houseRentAllowance + dearnessAllowance +
medicalAllowance;
double lessPay = (leave <= 5) ? (0.25 * basic) : (0.5 * basic);
double netPay = totalAmount - lessPay;
System.out.println("工资总额:" + totalAmount);
System.out.println("净工资:" + netPay);
}
}

class Manager
extends Employee {
String department;
public Manager(String name,double basic,String address,String department){
super(name,basic,address);
this.department=department;
}
public void show(){
super.show();
System.out.println("部门:"+department);
}
public void wages() {
}
}


----------------解决方案--------------------------------------------------------

我把程序全部重写了!终于通过了呵呵!

abstract class Employee {
String name;
double basic;
String address;
double leave;
double lessPay;
public Employee(String name, double basic, String address) {
this.name = name;
this.basic = basic;
this.address = address;
}

public void show() {
System.out.println("姓名:" + name);
System.out.println("地址:" + address);
System.out.println("薪资:" + basic);
}

public abstract double wages();

public double deduct(double leave) {
double lessPay = (leave <= 5) ? (0.25 * basic) : (0.5 * basic);
return lessPay;
}
}

class Manager
extends Employee {
String department;
public Manager(String name, double basic, String address, String department) {
super(name, basic, address);
this.department = department;
}

public void show() {
super.show();
System.out.println("部门:" + department);
}

public double wages() {
double houseRentAllowance = basic * 0.08;
double dearnessAllowance = basic * 0.3;
double medicalAllowance = 1500;
double totalAmount = basic + houseRentAllowance + dearnessAllowance +
medicalAllowance;
return totalAmount;
}
}


public class Director
extends Employee {
double transportAllowance;
double houseRentAllowance;
double dearnessAllowance;
double medicalAllowance;
double enterainmentAllowance;
double totalAmount;
public Director(String name, double basic, String address,double transportAllowance) {
super(name, basic, address);
this.transportAllowance=transportAllowance;
}

public void show() {
super.show();
}

public double wages() {
double houseRentAllowance = basic * 0.2;
double dearnessAllowance = basic * 0.5;
double medicalAllowance = 4500;
double enterainmentAllowance = 5000;
double totalAmount = basic + houseRentAllowance + dearnessAllowance +
medicalAllowance + enterainmentAllowance + transportAllowance;
return totalAmount;
}
}

我还有个问题 抽象方法中的属性是不是不能被重写啊?

[此贴子已经被作者于2006-9-9 16:39:48编辑过]


----------------解决方案--------------------------------------------------------
我刚才给你改的程序已经可以了

----------------解决方案--------------------------------------------------------
  相关解决方案