前言:
眼前小伙伴们对“javaif中return”都比较关心,各位老铁们都想要剖析一些“javaif中return”的相关知识。那么小编也在网上网罗了一些对于“javaif中return””的相关文章,希望朋友们能喜欢,小伙伴们快快来学习一下吧!使用多态或策略模式:将条件判断转换为对象的行为,减少 if-else 的使用。
public interface DiscountStrategy { double applyDiscount(double amount);}public class SeniorDiscountStrategy implements DiscountStrategy { @Override public double applyDiscount(double amount) { // 计算老年人优惠后的价格 return amount * 0.9; }}public class StudentDiscountStrategy implements DiscountStrategy { @Override public double applyDiscount(double amount) { // 计算学生优惠后的价格 return amount * 0.8; }}public class DiscountCalculator { private DiscountStrategy discountStrategy; public void setDiscountStrategy(DiscountStrategy discountStrategy) { this.discountStrategy = discountStrategy; } public double calculateDiscount(double amount) { return discountStrategy.applyDiscount(amount); }}// 使用示例DiscountCalculator calculator = new DiscountCalculator();calculator.setDiscountStrategy(new SeniorDiscountStrategy());double discountedPrice = calculator.calculateDiscount(100.0);使用工厂模式:通过工厂类创建对象,避免直接使用 if-else 进行对象的创建。
public interface DiscountStrategy { double applyDiscount(double amount);}public class SeniorDiscountStrategy implements DiscountStrategy { @Override public double applyDiscount(double amount) { // 计算老年人优惠后的价格 return amount * 0.9; }}public class StudentDiscountStrategy implements DiscountStrategy { @Override public double applyDiscount(double amount) { // 计算学生优惠后的价格 return amount * 0.8; }}public class DiscountStrategyFactory { public static DiscountStrategy createDiscountStrategy(String userType) { if (userType.equalsIgnoreCase("senior")) { return new SeniorDiscountStrategy(); } else if (userType.equalsIgnoreCase("student")) { return new StudentDiscountStrategy(); } return null; }}// 使用示例DiscountStrategy discountStrategy = DiscountStrategyFactory.createDiscountStrategy("senior");double discountedPrice = discountStrategy.applyDiscount(100.0);使用 Map 或策略映射:将条件与对应的操作存储在 Map 中,根据条件查找对应的操作进行执行。
public class DiscountCalculator { private Map<String, DiscountStrategy> discountStrategies; public DiscountCalculator() { discountStrategies = new HashMap<>(); discountStrategies.put("senior", new SeniorDiscountStrategy()); discountStrategies.put("student", new StudentDiscountStrategy()); } public double calculateDiscount(String userType, double amount) { DiscountStrategy discountStrategy = discountStrategies.get(userType); if (discountStrategy != null) { return discountStrategy.applyDiscount(amount); } return amount; }}// 使用示例DiscountCalculator calculator = new DiscountCalculator();double discountedPrice = calculator.calculateDiscount("senior", 100.0);
4、使用枚举优化
public enum DiscountType { SENIOR { @Override public double applyDiscount(double amount) { // 计算老年人优惠后的价格 return amount * 0.9; } }, STUDENT { @Override public double applyDiscount(double amount) { // 计算学生优惠后的价格 return amount * 0.8; } }; public abstract double applyDiscount(double amount);}// 使用示例DiscountType discountType = DiscountType.SENIOR;double discountedPrice = discountType.applyDiscount(100.0);
通过枚举类型,可以将不同条件的操作逻辑封装在枚举常量中,使用起来更加简洁和易于扩展。
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #javaif中return