| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 505 人关注过本帖
标题:继承问题
只看楼主 加入收藏
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
结帖率:33.33%
收藏
 问题点数:0 回复次数:8 
继承问题

我做的代码有问题可是我却不知道为什么会有错,代码如下:
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类不是抽象类不能被重写这个错误,这个是为什么啊??

搜索更多相关主题的帖子: 继承 
2006-09-09 12:25
神vLinux飘飘
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:浙江杭州
等 级:贵宾
威 望:91
帖 子:6140
专家分:217
注 册:2004-7-17
收藏
得分:0 
至少要有个和抽象方法一样的签名 public void wages(){}

淘宝杜琨
2006-09-09 12:39
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 
我在类中不是写了 public void wages(double transportAllowance, double leave){} 的吗?我是要在Director中重写父类中的wages方法! 如果不能这么写的话,那要怎么写啊?还有就是为什么我继承了父类后,用父类的这个show方法可是他却提示找不到这个符号啊?谢谢大大啊!

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



2006-09-09 12:45
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
收藏
得分:0 
在3个类都去掉public
Director类中写上public void wages(){}

win32汇编
病毒 加密
目前兴趣所在
2006-09-09 13:32
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 

无效!


2006-09-09 14:21
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
收藏
得分:0 

我在我这通过编译了


win32汇编
病毒 加密
目前兴趣所在
2006-09-09 14:23
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
收藏
得分:0 

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() {
}
}


win32汇编
病毒 加密
目前兴趣所在
2006-09-09 14:26
lgdcky
Rank: 2
等 级:论坛游民
威 望:5
帖 子:576
专家分:18
注 册:2006-8-5
收藏
得分:0 

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

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编辑过]


2006-09-09 16:38
无理取闹
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:53
帖 子:4264
专家分:0
注 册:2006-7-26
收藏
得分:0 
我刚才给你改的程序已经可以了

win32汇编
病毒 加密
目前兴趣所在
2006-09-09 16:45
快速回复:继承问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029066 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved