我不是个伟大的程序员;我只是个有着一些优秀习惯的好程序员而己
本人比较直接,不说虚的,直接上干货。
目录
public double getPrice() {
int basePrice = _quantity * _itemPrice;
int discountLevel;
if (_quantity > 100) discountLevel = 2;
else discountLevel = 1;
double finalPrice = discountedPrice (basePrice, discountLevel);
return finalPrice;
}
private double discountedPrice (int basePrice, int discountLevel) {
if (discountLevel == 2) return basePrice * 0.1;
else return basePrice * 0.05;
}
优化 1,2,3步骤 优化参数basePrice
public double getPrice() {
int basePrice = getBasePrice();
int discountLevel;
if (_quantity > 100) discountLevel = 2;
else discountLevel = 1;
double finalPrice = discountedPrice ( discountLevel);
return finalPrice;
}
private double discountedPrice ( int discountLevel) {
if (discountLevel == 2) return getBasePrice() * 0.1;
else return getBasePrice() * 0.05;
}
private int getBasePrice(){
return _quantity * _itemPrice;
}
优化4步骤 去掉参数basePrice
public double getPrice() {
int discountLevel;
if (_quantity > 100) discountLevel = 2;
else discountLevel = 1;
double finalPrice = discountedPrice ( discountLevel);
return finalPrice;
}
private double discountedPrice ( int discountLevel) {
if (discountLevel == 2) return getBasePrice() * 0.1;
else return getBasePrice() * 0.05;
}
private int getBasePrice(){
return _quantity * _itemPrice;
}
优化1,2,3,4步骤,去掉discountLevel参数,独立函数返回值要为discountLevel 最后赋值的值
public double getPrice() {
double finalPrice = discountedPrice ();
return finalPrice;
}
private double discountedPrice () {
if (getDiscountLevel() == 2) return getBasePrice() * 0.1;
else return getBasePrice() * 0.05;
}
private double getDiscountLevel(){
if (_quantity > 100) return 2;
else return 1;
}
private double getBasePrice(){
return _quantity * _itemPrice;
}
从上述代码可看出,getPrice主函数finalPrice参数已经可以直接优化了。
public double getPrice() {
return discountedPrice ();
}
public double getPrice() {
if (getDiscountLevel() == 2) return getBasePrice() * 0.1;
else return getBasePrice() * 0.05;
}
private double getDiscountLevel(){
if (_quantity > 100) return 2;
else return 1;
}
private double getBasePrice(){
return _quantity * _itemPrice;
}
public class Car{
// 奔驰
public void printBenz(String brand, String model, Integer price, double power) {
printBasicInfo(brand, model, price, power);
getTax(power, price);
}
// 宝马
public void printBmw(String brand, String model, Integer price, double power) {
printBasicInfo(brand, model, price, power);
getTax(power, price);
}
// 提炼打印基本信息方法
private void printBasicInfo(String brand, String model, Integer price, double power) {
System.out.println("品牌" + brand);
System.out.println("型号:" + model);
System.out.println("动力:" + power);
System.out.println("价格:" + price);
}
// 提炼计算税费的方法
private double getTax(double power, Integer price){
double salePrice = price;
if (price > 200000) {
salePrice = price * 0.98;
}
if (power <= 1.6) {
return salePrice * 0.05;
} else {
return salePrice * 0.1;
}
}
}
public class CarEntity {
private String brand;
private String model;
private Integer price;
private Double power;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Double getPower() {
return power;
}
public void setPower(Double power) {
this.power = power;
}
}
优化3,4步骤
// 奔驰
public void printBenz(CarEntity carEntity) {
printBasicInfo(carEntity);
// 计算税费
getTax(carEntity);
}
// 宝马
public void printBmw(CarEntity carEntity) {
printBasicInfo(carEntity);
getTax(carEntity);
}
private void printBasicInfo(CarEntity carEntity) {
System.out.println("品牌" + carEntity.getBrand());
System.out.println("型号:" + carEntity.getModel());
System.out.println("动力:" + carEntity.getPower());
System.out.println("价格:" + carEntity.getPrice());
}
// 计算税费
private double getTax(CarEntity carEntity) {
// 打折后价格
double salePrice = carEntity.getPrice();
if (carEntity.getPrice() > 200000) {
salePrice = carEntity.getPrice() * 0.98;
}
if (carEntity.getPower() <= 1.6) {
return salePrice * 0.05;
} else {
return salePrice * 0.1;
}
}
String[] row = new String[3];
row [0] = "Liverpool";
row [1] = "15";
//对象取代数组
Performance row = new Performance();
row.setName("Liverpool");
row.setWins("15");
作者:小虚竹
欢迎任何形式的转载,但请务必注明出处。
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。