请老师帮忙看看有没有什么问题,哪里需要优化?
来源:2-23 编程练习
LLLL_
2019-10-01 17:03:24
package com.lxb.bclx;
public class NonMotor {
//创建私有属性
private String brand;
private String color;
private int chelun=2;
private int zuoyi=1;
//创建无参构造
public NonMotor() {
}
//创建双参构造
public NonMotor(String brand,String color) {
this.setBrand(brand);
this.setColor(color);
}
//创建四参构造方法
public NonMotor(String brand,String color,int chelun,int zuoyi ) {
this.setBrand(brand);
this.setColor(color);
this.setZuoyi(zuoyi);
this.setChelun(chelun);
}
//创建get/set方法,完成属性封装
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getChelun() {
return chelun;
}
public void setChelun(int chelun) {
this.chelun = chelun;
}
public int getZuoyi() {
return zuoyi;
}
public void setZuoyi(int zuoyi) {
this.zuoyi = zuoyi;
}
//创建方法
public String work() {
String str="这是一辆"+this.getColor()+"颜色的,"
+ this.getBrand()+"牌的非机动车,有"+this.getChelun()+""
+ "个轮子,有"+this.getZuoyi()+"个座椅";
return str;
}
}
////////////////////////////////////////////////////////////////////////////////////////
package com.lxb.bclx;
public class Bicycle extends NonMotor{
//编写无参构造
public Bicycle() {
}
//编写并继承父类构造
public Bicycle(String brand,String color) {
super(brand,color);
this.setBrand(brand);
this.setColor(color);
}
//方法重写
public String work() {
String str="这是一辆"+this.getColor()+"的,"
+ this.getBrand()+"牌子的自行车";
return str;
}
}
///////////////////////////////////////////////////////////////////////////////////////
package com.lxb.bclx;
public class ElectricVehicle extends NonMotor{
//创建私有属性
private String dcbrand;
//创建无参构造方法
public ElectricVehicle() {
}
// //创建有参构造方法
// public ElectricVehicle(String dcbrand) {
// //super();
// }
//创建get/set方法
public String getDcbrand() {
return dcbrand;
}
public void setDcbrand(String dcbrand) {
this.dcbrand = dcbrand;
}
//方法重写
public String work() {
String str="这是一辆使用"+this.getDcbrand()+"牌电池的电动车";
return str;
}
}
//////////////////////////////////////////////////////////////////////////////////////////
package com.lxb.bclx;
public class Tricycle extends NonMotor{
//创建无参构造
public Tricycle() {
super.setChelun(3);
}
//方法重写
public String work() {
String str="三轮车是一款有"+this.getChelun()+"个轮子的非机动车";
return str;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
package com.lxb.bclx;
public class Test {
public static void main(String[] args) {
//父类测试信息
System.out.print("父类测试信息:");
NonMotor n=new NonMotor("天宇","红");
System.out.println(n.work());
//自行车类测试信息
System.out.print("自行车类测试信息:");
Bicycle b=new Bicycle("捷安特","黄色");
System.out.println(b.work());
//电动车测试类信息
System.out.print("电动车测试类信息:");
ElectricVehicle e=new ElectricVehicle();
e.setDcbrand("飞鸽");
System.out.println(e.work());
//三轮车测试类信息
System.out.print("三轮车测试类信息:");
Tricycle t=new Tricycle();
System.out.println(t.work());
}
}我的小问题: 请老师分别回答我谢谢 ~~
1、 在编程练习中,以为操作步骤给的比较详细,我会按照顺序去打代码
但是如果在实际编程中,是一个怎样的过程呢?或者说,什么是比较合理的开发顺序呢?
2、请老师着重看我 ElectricVehicle.java中 注释掉的有参构造方法,我发现及时不用有参构造(不用有参构造的super关键字),也可以在test.java中进行测试通过 。 因为它不需要像Bicycle.java中 需要通过super()去调用父类的属性。 所以我的想问,是不是我恰好打出了,而不是用的需要练习的方法。
3、不知道我的编程是否符合面向对象的思想 ?
拜托老师一一回答 ,非常感谢老师~~谢谢!
1回答
同学你好,1. 在实际开发环境中拿到需求后,首先分析需求再编辑代码。与任务分析是一致的。只是在编程练习中有任务分析帮助实现功能。在实际开发需要自己分析。
2. 是的,通过set方法设置值,也是可以完成功能的。
3.同学编程是符合面向对象的思想的。
4. 同学已完成练习,棒棒哒!继续加油!如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
相似问题