老师帮我看看我的代码对吗,还有哪里需要改进吗?
来源:2-8 编程练习
慕仙7354812
2019-12-29 19:32:59
package com.imooc.homework;
public class TestWork extends Work {
// 属性:编写的测试用例个数、发现的Bug数量
private int number;
private int bug;
// 编写构造方法,并调用父类相关赋值方法,完成属性赋值
// 无参构造
public TestWork() {
}
//带参构造
public TestWork(String name,int number,int bug) {
this.setName(name);
this.setNumber(number);
this.setBug(bug);
}
// 公有的get***/set***方法完成属性封装
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getBug() {
return bug;
}
public void setBug(int bug) {
this.bug = bug;
}
// 重写运行方法,描述内容为:**的日报是:今天编写了**个测试用例,发现了**bug。其中**的数据由属性提供
public String work() {
System.out.println(this.getName() + "的日报是:今天编写了" + this.getNumber() + "个测试用例,发现了" + this.getBug() + "bug。其中"
+ this.getName() + "的数据由属性提供");
return "";
}
}
public class DevelopmentWork extends Work {
// 属性:有效编码行数、目前没有解决的Bug个数
private int line;
private int stillBug;
// 编写构造方法,并调用父类相关赋值方法,完成属性赋值
public DevelopmentWork() {
}
public DevelopmentWork(String name,int line,int stillBug) {
this.setName(name);
this.setLine(line);
this.setStillBug(stillBug);
}
// 公有的get***/set***方法完成属性封装
public int getLine() {
return line;
}
public void setLine(int line) {
this.line = line;
}
public int getStillBug() {
return stillBug;
}
public void setStillBug(int stillBug) {
this.stillBug = stillBug;
}
// 重写运行方法,描述内容为:**的日报是:今天编写了**行代码,目前仍然有**个bug没有解决。其中**的数据由属性提供
public String work() {
System.out.println(this.getName() + "的日报是:今天编写了" + this.getLine() + "行代码,目前仍然有" + this.getStillBug()
+ "个bug没有解决。其中" + this.getName() + "的数据由属性提供");
return "";
}
}
public class Work {
// 属性:工作ming
private String name ;
// 无参构造方法
public Work() {
}
// 带参构造方法,完成工作类型的赋值
public Work(String name) {
this.setName(name);
}
// 公有的get***/set***方法完成属性封装
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 方法:工作描述,描述内容为:开心工作
public String work() {
System.out.println("开心工作");
return "";
}
}
public class Test {
public static void main(String[] args) {
System.out.print("父类信息测试:");
Work work1=new Work();
work1.work();
System.out.print("测试工作类信息测试:");
TestWork testwork=new TestWork();
testwork.setName("测试工作");
testwork.setNumber(10);
testwork.setBug(5);
testwork.work();
System.out.print("研发工作类信息测试:");
DevelopmentWork dep=new DevelopmentWork();
dep.setName("研发工作");
dep.setLine(1000);
dep.setStillBug(10);
dep.work();
}
}1回答
同学的程序正确,运行无误。
同学根据题意进行改进如可以将set方法传进的值以带参构造方法来完成属性赋值,具体修改如下图所示:


![]()
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~
相似问题