2-8编程练习
import com.imooc.wtask.DevelopmentWork;
import com.imooc.wtask.TestWork;
import com.imooc.wtask.Work;
public class TestW {
public static void main(String[] args) {
//测试
System.out.print("父类信息测试:");
Work one=new Work("测试工作");
one.WorkInfor();
System.out.print("测试工作类信息测试:");
TestWork a=new TestWork(10,5,"测试工作");
a.WorkInfor();
System.out.print("研发工作类信息测试:");
DevelopmentWork b=new DevelopmentWork(1000,5,"研发工作");
b.WorkInfor();
}
}public class Work {
//编写无参构造方法、带参构造方法完成对属性的赋值
private String workName;
public Work() {
}
public Work(String workName) {
this.setWorkName(workName);
}
public String getWorkName() {
return workName;
}
public void setWorkName(String workName) {
this.workName = workName;
}
//编写工作描述的方法,描述内容为:开心工作
public void WorkInfor() {
System.out.println("开心工作");
}
}public class TestWork extends Work {
//增加属性:编写的测试用例个数、发现的Bug数量
private int caseCount;
private int bugCount;
//在构造方法中调用父类相关赋值方法,完成属性赋值
public TestWork() {
}
public TestWork(int caseCount,int bugCount,String workName) {
this.setCaseCount(caseCount);
this.setBugCount(bugCount);
this.setWorkName(workName);
}
// 公有的get/set方法完成属性封装
public int getCaseCount() {
return caseCount;
}
public void setCaseCount(int caseCount) {
this.caseCount = caseCount;
}
public int getBugCount() {
return bugCount;
}
public void setBugCount(int bugCount) {
this.bugCount = bugCount;
}
//重写运行方法,描述内容为:**的日报是:今天编写了**个测试用例,发现了**bug。其中**的数据由属性提供
public void WorkInfor() {
System.out.println(this.getWorkName()+"的日报是:今天编写了"+this.getCaseCount()+"个测试用例,发现了"+this.getBugCount()+"bug");
}
}public class DevelopmentWork extends Work {
//增加属性:有效编码行数、目前没有解决的Bug个数
private int lineCount;
private int unsolvedBug;
//在构造方法中调用父类相关赋值方法,完成属性赋值
public DevelopmentWork() {
}
public DevelopmentWork(int lineCount,int unsolvedBug,String workName) {
this.setWorkName(workName);
this.setLineCount(lineCount);
this.setUnsolvedBug(unsolvedBug);
}
public int getLineCount() {
return lineCount;
}
public void setLineCount(int lineCount) {
this.lineCount = lineCount;
}
public int getUnsolvedBug() {
return unsolvedBug;
}
public void setUnsolvedBug(int unsolvedBug) {
this.unsolvedBug = unsolvedBug;
}
//重写运行方法,描述内容为:**的日报是:今天编写了**行代码,目前仍然有**个bug没有解决。其中**的数据由属性提供
public void WorkInfor() {
System.out.println(this.getWorkName() + "的日报是:今天编写了" + "行代码,目前仍然有" + this.getUnsolvedBug() + "个bug没有解决。");
}老师这是我完成的代码 运行正常 有没有需要改进的地方?
0
收起
正在回答 回答被采纳积分+1
2回答
慕后端4084011
2019-09-29 18:32:28
又看了下别的同学的代码 发现我有个问题 在写父类方法以及其他重写方法时 应该用带返回值String类型的方法 并返回 之前课中老师讲过 结果写的时候忘了 顺便问一句这种写法是为了优化代码吗?
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星