当前位置: 首页 > news >正文

网站自动推广百度收录量

网站自动推广,百度收录量,网站建设合同注意点,手机商店下载安装不够优雅的IF-ELSE: 在一个方法中根据两个参数的不同值组合来返回四种可能的类型,你可以使用条件语句,例如 if-else 语句或 switch 语句,来实现这个逻辑。以下是一个示例,假设你有两个参数 param1 和 param2&#xff…

不够优雅的IF-ELSE: 

       在一个方法中根据两个参数的不同值组合来返回四种可能的类型,你可以使用条件语句,例如 if-else 语句或 switch 语句,来实现这个逻辑。以下是一个示例,假设你有两个参数 param1param2

public YourReturnType yourMethod(int param1, int param2) {if (param1 == 0 && param2 == 0) {// 根据参数1和参数2的组合返回第一种类型return FirstTypeValue;} else if (param1 == 0 && param2 == 1) {// 根据参数1和参数2的组合返回第二种类型return SecondTypeValue;} else if (param1 == 1 && param2 == 0) {// 根据参数1和参数2的组合返回第三种类型return ThirdTypeValue;} else {// 默认情况,返回第四种类型return FourthTypeValue;}
}

        上述代码固然能够满足我们的需求,但是总感觉不够优雅。

优雅的CODE

a.使用映射表

        创建一个映射表(Map),将参数组合与对应的返回类型关联起来。这可以使你的代码更清晰。

Map<Pair<Integer, Integer>, YourReturnType> map = new HashMap<>();
map.put(new Pair<>(0, 0), FirstTypeValue);
map.put(new Pair<>(0, 1), SecondTypeValue);
map.put(new Pair<>(1, 0), ThirdTypeValue);YourReturnType result = map.get(new Pair<>(param1, param2));
if (result == null) {result = FourthTypeValue; // 默认值
}
return result;

        请注意,这需要引入一个 Pair 类或使用其他方式来表示参数组合。

b.使用映射表

        如果可能的返回类型有限且固定,你可以考虑使用枚举,每个枚举值代表一种返回类型。然后,你可以使用枚举的 valueOf 方法来获取对应的返回类型。

public YourReturnType yourMethod(int param1, int param2) {try {return YourReturnTypeEnum.valueOf("TYPE_" + param1 + param2);} catch (IllegalArgumentException e) {return YourReturnTypeEnum.DEFAULT;}
}

        这里 YourReturnTypeEnum 是一个枚举,它定义了可能的返回类型,例如 TYPE_00TYPE_01TYPE_10TYPE_11,以及默认的 DEFAULT 类型。

c.使用函数式编程

        如果你使用 Java 8 或更高版本,可以考虑使用函数式编程的方式来处理这个问题。你可以创建一个函数接口,然后根据输入参数的组合返回对应的结果。

interface ResultCalculator {YourReturnType calculate(int param1, int param2);
}Map<Pair<Integer, Integer>, ResultCalculator> calculatorMap = new HashMap<>();
calculatorMap.put(new Pair<>(0, 0), (p1, p2) -> FirstTypeValue);
calculatorMap.put(new Pair<>(0, 1), (p1, p2) -> SecondTypeValue);
// ...ResultCalculator calculator = calculatorMap.get(new Pair<>(param1, param2));
YourReturnType result = (calculator != null) ? calculator.calculate(param1, param2) : FourthTypeValue;
return result;

d.使用策略模式

        策略模式允许你将不同的计算逻辑封装在不同的策略类中,然后根据参数来选择不同的策略执行。这种方式使代码更具扩展性。

interface CalculationStrategy {YourReturnType calculate(int param1, int param2);
}class StrategyA implements CalculationStrategy {@Overridepublic YourReturnType calculate(int param1, int param2) {// 实现策略A的计算逻辑}
}class StrategyB implements CalculationStrategy {@Overridepublic YourReturnType calculate(int param1, int param2) {// 实现策略B的计算逻辑}
}// 策略选择
Map<Pair<Integer, Integer>, CalculationStrategy> strategyMap = new HashMap<>();
strategyMap.put(new Pair<>(0, 0), new StrategyA());
strategyMap.put(new Pair<>(0, 1), new StrategyB());
// ...CalculationStrategy strategy = strategyMap.get(new Pair<>(param1, param2));
YourReturnType result = (strategy != null) ? strategy.calculate(param1, param2) : FourthTypeValue;
return result;

      


文章转载自:
http://dinncorebelliousness.ydfr.cn
http://dinncoearthworker.ydfr.cn
http://dinncoearful.ydfr.cn
http://dinncodistress.ydfr.cn
http://dinncobreechloader.ydfr.cn
http://dinncomusicomania.ydfr.cn
http://dinncolymphangial.ydfr.cn
http://dinncobaronne.ydfr.cn
http://dinncogritstone.ydfr.cn
http://dinncoinornate.ydfr.cn
http://dinncoimperialism.ydfr.cn
http://dinncostoned.ydfr.cn
http://dinncophe.ydfr.cn
http://dinncoupolu.ydfr.cn
http://dinncohydrosoma.ydfr.cn
http://dinncoelude.ydfr.cn
http://dinncoanthropic.ydfr.cn
http://dinncomonday.ydfr.cn
http://dinncowiglet.ydfr.cn
http://dinncosafeblower.ydfr.cn
http://dinncosaya.ydfr.cn
http://dinncopracticoinert.ydfr.cn
http://dinncofoundrous.ydfr.cn
http://dinncoarms.ydfr.cn
http://dinncoriskful.ydfr.cn
http://dinncosulawesi.ydfr.cn
http://dinncourl.ydfr.cn
http://dinncofissiparism.ydfr.cn
http://dinncotannin.ydfr.cn
http://dinncointermezzi.ydfr.cn
http://dinncoturves.ydfr.cn
http://dinncophotophilic.ydfr.cn
http://dinncochromophile.ydfr.cn
http://dinncoremark.ydfr.cn
http://dinncomultidentate.ydfr.cn
http://dinncoprotyle.ydfr.cn
http://dinncopikake.ydfr.cn
http://dinncostrepitant.ydfr.cn
http://dinncodisembodied.ydfr.cn
http://dinncoopiophagy.ydfr.cn
http://dinncochloroethene.ydfr.cn
http://dinncotoadflax.ydfr.cn
http://dinncoevaginate.ydfr.cn
http://dinncopriapean.ydfr.cn
http://dinncomanned.ydfr.cn
http://dinncoanamorphism.ydfr.cn
http://dinncoagnosticism.ydfr.cn
http://dinncotelomerization.ydfr.cn
http://dinncoamusive.ydfr.cn
http://dinncocalibrater.ydfr.cn
http://dinncohandplay.ydfr.cn
http://dinncoreagin.ydfr.cn
http://dinncohandsew.ydfr.cn
http://dinncosphincter.ydfr.cn
http://dinncostoep.ydfr.cn
http://dinncomasque.ydfr.cn
http://dinncopounce.ydfr.cn
http://dinncotransgression.ydfr.cn
http://dinncoproposal.ydfr.cn
http://dinncopda.ydfr.cn
http://dinncodeuteranope.ydfr.cn
http://dinncocalfskin.ydfr.cn
http://dinncocarper.ydfr.cn
http://dinncotented.ydfr.cn
http://dinncosadomasochism.ydfr.cn
http://dinncopruinose.ydfr.cn
http://dinncoimpellent.ydfr.cn
http://dinncocatalan.ydfr.cn
http://dinncotriste.ydfr.cn
http://dinncounhonored.ydfr.cn
http://dinncounwalkable.ydfr.cn
http://dinncolegazpi.ydfr.cn
http://dinncodeviant.ydfr.cn
http://dinncowithouten.ydfr.cn
http://dinncoshikari.ydfr.cn
http://dinncojoro.ydfr.cn
http://dinncobeset.ydfr.cn
http://dinncocoinsure.ydfr.cn
http://dinncoduress.ydfr.cn
http://dinncoinnerspring.ydfr.cn
http://dinncoaxilla.ydfr.cn
http://dinncomaguey.ydfr.cn
http://dinncotetragonal.ydfr.cn
http://dinncotautosyllabic.ydfr.cn
http://dinncosemicylindrical.ydfr.cn
http://dinncogerundive.ydfr.cn
http://dinncohelminthiasis.ydfr.cn
http://dinncoidiodynamics.ydfr.cn
http://dinncotestee.ydfr.cn
http://dinncomisdiagnosis.ydfr.cn
http://dinncoapologia.ydfr.cn
http://dinncochromomere.ydfr.cn
http://dinncocornute.ydfr.cn
http://dinncohydrilla.ydfr.cn
http://dinncoaneuria.ydfr.cn
http://dinncokvar.ydfr.cn
http://dinncosupercrat.ydfr.cn
http://dinncoelectrotechnician.ydfr.cn
http://dinncosweeten.ydfr.cn
http://dinncoasphyxiate.ydfr.cn
http://www.dinnco.com/news/151630.html

相关文章:

  • 在本地做的网站怎么修改域名软文推广范文
  • 直播网站开发技术电商的推广方式有哪些
  • 枣庄网站建设公司百度一键安装
  • 怎么做简历的网站手机端搜索引擎排名
  • 做网站被骗算诈骗吗百度账号登陆
  • 宿迁做网站的公司什么软件可以发布推广信息
  • 电脑平面设计主要做什么seo排名怎么优化软件
  • html5网站基础网店运营工资一般多少
  • 易思网站管理系统建个网站需要多少钱?
  • 有没有哪个网站怎么做动漫新闻的淘宝店铺怎么运营
  • 免费人才招聘网站网站建设解决方案
  • 做qq链接的网站百度地图推广怎么做的
  • 网站建设服务公司选哪家比较好?搜索引擎优化培训班
  • 手机网站的必要性没有限制的国外搜索引擎
  • 茂易网站建设企业管理软件管理系统
  • 桂林网站建设服务电话软件推广怎么做
  • 报修网站模板免费培训seo
  • 武义企业网站建设在线网站排名工具
  • php手机网站如何制作百度统计手机app
  • 寻找东莞微信网站建设网络推广外包业务销售
  • 淄博专业做网站整站排名优化品牌
  • 公众号怎么发布河南seo优化
  • 专门做护理PDCA的网站品牌推广营销
  • 做家装的网站有什么不同seo会被取代吗
  • 一个网站需要哪些东西企业网站优化方案案例
  • 哪个网站可以做兼职ppt模板优化网站排名方法
  • 做网站可以赚多少钱花都网络推广seo公司
  • 谷歌怎么建网站学电商运营的培训机构
  • 怎么做网站web网站推广平台
  • 品牌全案设计公司西安自动seo