我定义了一些资源,例如:
<color name="lightGrey">#FFCCCCCC</color>
<integer name="KEY_POSITION_ARM">2</integer>
Run Code Online (Sandbox Code Playgroud)
...我有一个ArrayAdapter向TextViews显示项目.我正在尝试使用以下代码访问值:
keyPosition = getResources().getInteger(R.integer.KEY_POSITION_ARM);
moduleDataView.setTextColor(getResources().getColor(R.color.darkgreen));
Run Code Online (Sandbox Code Playgroud)
...但是我得到的错误是"方法getResources()未定义类型ContinuityAdapter".(ContinuityAdapter扩展ArrayAdapter)
这有什么好办法吗?
谢谢
这是一个例子:
switch (currentModule.keyPosition) {
case activity.getResources().getInteger(R.integer.KEY_POSITION_ARM):
moduleDataView.keyPosition.setText("TEST");
moduleDataView.keyPosition.setTextColor(Color.GREEN);
break;
case R.integer.KEY_POSITION_ARM:
moduleDataView.keyPosition.setText("ARM");
moduleDataView.keyPosition.setTextColor(Color.RED);
break;
}
Run Code Online (Sandbox Code Playgroud)
第一种情况给出错误,第二种情况不会,但也不使用XML文件中的值.虽然正如你所说,我可以只使用R ...值,只要我在任何地方使用它.只是不确定这是否被认为是"最佳实践".谢谢
对于项目,我们有一个Controller/Service/DAO架构.我们实现了对不同提供者API的调用,因此我们在每个控制器类中都得到了一些这样的样板代码:
enum {
PARTNER_A, PARTNER_B, PARTNER_C
}
public class MyController {
@Resource PartnerASearchService partnerASearchService;
@Resource PartnerBSearchService partnerBSearchService;
@Resource PartnerCSearchService partnerCSearchService;
public search(InputForm form) {
switch(form.getPartnerName()) {
case PARTNER_A: partnerASearchService.search();
break;
case PARTNER_B: partnerBSearchService.search();
break;
case PARTNER_C: partnerCSearchService.search();
break;
}
}
public otherMethod(InputForm form) {
switch(form.getProvider()) {
case PARTNER_A: partnerAOtherService.otherMethod();
break;
case PARTNER_B: partnerBOtherService.otherMethod();
break;
case PARTNER_C: partnerCOtherService.otherMethod();
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用哪种设计模式来摆脱每个控制器中的这个开关?我希望代码类似于下面的代码:
public class MyController {
@Resource ServiceStrategy serviceStrategy;
public search(InputForm form){
serviceStrategy.search(form.getPartnerName())
// or
serviceStrategy.invoke(SEARCH, form.getPartnerName())
}
public otherMethod(InputForm …Run Code Online (Sandbox Code Playgroud) 我在switch()语句中处理了很多情况,并且想知道是否有任何可能的方式可以缩短这些情况。它们在我的代码中占用了很多空间,并且当这些语句有3-4个大块时,很难导航。这是一个例子:
...important lines of code...
void foo(string bar, bool blam) {
int v1 = stoi(bar);
switch (v1) {
case(11):
if(blam) {
exArr[1] = "A";
} else {
exArr[1] = "B";
}
break;
case(12):
if(blam) {
exArr[3] = "A";
} else {
exArr[3] = "B";
}
break;
...many more cases...
default:
printElement();
break;
}
...even more important code, which is dependent on the hard code above and hard to navigate...
Run Code Online (Sandbox Code Playgroud)
我认为您看到了问题。你们有什么建议吗?提前致谢。
重要编辑:
仅前12次迭代会更改exArr的字符。之后,它将更改为另一个(现有的)数组,例如ndArr,需要另外12次迭代。这种情况适用于4个数组,因此大约有48个case语句。
我刚刚完成了James Kovac 关于控制和依赖注入反转的文章,然后使用我学到的知识来制作我自己的IoC/DI示例.
我很满意这个例子,因为它:
但是,从这里开始,有些事情似乎很奇怪:
感谢您提供的任何方向.
using System;
using System.Linq;
using System.Collections.Generic;
namespace TestSimpleDependencyInjection1
{
class Program
{
static void Main(string[] args)
{
AuthorizationService authorizationService = new AuthorizationService();
//real example
Repository repository = new Repository(authorizationService);
for (int id = 1; id <= 3; id++)
{
Customer customer = repository.GetCustomer(id);
customer.Display();
}
Console.WriteLine();
//mock test example
MockRepository mockRepository = new MockRepository(authorizationService); …Run Code Online (Sandbox Code Playgroud) 我见过类似的线程,但是,不知道如何将解决方案完全应用到我的案例中.我的问题是我有一组用例可以说'A','B','C',当输入传递时需要执行某些命令(2个用例是输入)是列出的任何2个用例.例如:
switch(input1)
{
case A:
break;
case B:
break;
case C:
break;
}
Run Code Online (Sandbox Code Playgroud)
在每种情况下,我将不得不检查输入2,因此,最终的代码可能看起来像
switch(input1)
{
case A:
{
switch(input2):
case B:
break;
case c:
break;
}
case B:
{
switch(input2):
case A:
break;
case c:
break;
}
....
}
Run Code Online (Sandbox Code Playgroud)
我正在考虑使用(pair,command)的地图并删除这个开关案例,但有没有其他更好的解决方案或设计问题来解决这个问题?
我必须在不同的功能中重复使用相同的开关盒.在不同的功能中,开关盒的定义会有所不同.例如:
int groupID = somenumber;
private void FunctionA()
{
switch (groupID)
{
case 0:
// Do Action A
break;
case 1:
// Do Action B
break;
case 2:
// Do Action C
break;
}
}
private void FunctionB()
{
switch (groupID)
{
case 0:
// Do Action Z
break;
case 1:
// Do Action X
break;
case 2:
// Do Action Y
break;
}
}
Run Code Online (Sandbox Code Playgroud)
这是一次使用相同开关盒的任何方法但定义可能不同吗?
我正在研究一个税收计算器,并且想要将税率括号放入多维数组中.这是我第一次尝试这样的事情,并想知道这是否是正确的做法?
private static double[][][] taxBrackets;
static {
taxBrackets = new double[20][14][14];
/*Single*/ /*Married*/ /*Head of Household*/
//TierOne //TierOne //TierOne
taxBrackets[0][0][0] = 0; taxBrackets[0][1][0] = 0; taxBrackets[0][0][1] = 0; //MIN
taxBrackets[1][0][0] = 9075; taxBrackets[0][2][0] = 18150; taxBrackets[0][0][2] = 12950; //MAX
taxBrackets[2][0][0] = 0.10; //Tax Rate
//TierTwo
taxBrackets[3][0][0] = 9076; taxBrackets[0][3][0] = 18151; taxBrackets[0][0][3] = 12951; //MIN
taxBrackets[4][0][0] = 36900; taxBrackets[0][4][0] = 73800; taxBrackets[0][0][4] = 49400; //MAX
taxBrackets[5][0][0] = 0.15; //Tax Rate
//TierThree
taxBrackets[6][0][0] = 36901; taxBrackets[0][5][0] = 73801; taxBrackets[0][0][5] = 49401; …Run Code Online (Sandbox Code Playgroud) 当我们有5个切换用例时,switch语句非常好,但是在我们的情况下,我们有15个切换用例。
我想知道什么是Java中switch语句的最佳替代选择
private OpwaardernId getOpwaardernId(String opId) {
OpwaardernId opwaardernId;
switch (opId) {
case "ID001":
opwaardernId = opwaardernId.of("xxx");
break;
case "ID002":
opwaardernId = opwaardernId.of("123");
break;
case "ID002":
opwaardernId = opwaardernId.of("abc");
break;
case "ID003":
opwaardernId = opwaardernId.of("asd");
break;
case "ID004":
opwaardernId = opwaardernId.of("rrr");
break;
case "ID005":
opwaardernId = opwaardernId.of("ttt");
break;
...
case "ID015":
opwaardernId = opwaardernId.of("aaa");
break;
default:
opwaardernId = null;
}
return opwaardernId;
}
Run Code Online (Sandbox Code Playgroud) 我想在明天的办公时间去问老师之前得到一些信息.
我们有项目可以做类似iclicker问题回答收集器的事情.他告诉我们要避免使用switch case语句.我只是想知道为什么以及为什么这个领域的人不喜欢使用它们,还有什么选择呢?而且我怀疑他是否希望我们使用if语句.
我认为我们必须使用多态/接口,但我只是不敢说清楚,切换案例似乎非常直接.
谢谢.
我有这个switch语句,在用户显示要执行的操作列表并单击其中一个之后执行.我们打开的是动作ID
switch (actionId) {
case 0:
openEditProductScreen();
break;
case 1:
startDeleteProductOperation();
break;
case 2:
//nothing
break;
case 3:
openAddProductScreen();
break;
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了一些关于用多态替换开关的文章,但它们与另一种类型的问题有关 - 以不同的方式做同样的事情(你支付不同类型的员工的方式),但是当我想触发完全不同的时候我该怎么办一系列行动?
想一想,我真的需要消除这种特殊的切换声明吗?我的意思是,它具有可读性和逻辑性.如果我以某种方式消除它会带来什么好处?
编辑:
这是你的意思吗?
private Map<Integer, ProductRelatedAction> productRelatedActions = new HashMap<Integer, ProductRelatedAction>();
private void mapActionsToIds() {
productRelatedActions.put(0, new EditProductAction());
productRelatedActions.put(1, new DeleteProductAction());
// remainder omitted
}
private abstract class ProductRelatedAction{
abstract void execute();
}
private class EditProductAction extends ProductRelatedAction{
@Override
void execute() {
openEditProductScreen();
}
}
private class DeleteProductAction extends ProductRelatedAction{
@Override
void execute() { …Run Code Online (Sandbox Code Playgroud) 我编写了以下代码,首先生成0-23之间的随机值(该值表示第0天的时间表示00:00 am).然后它生成0-6之间的第二个值,表示0表示星期日,6表示星期六.在初始化一天中的时间和一周中的某一天之后,将调用方法速度以获得该特定时间的汽车速度.有没有其他方法可以改进编写此代码的方式.
主要方法:
Random r = new Random();
int time_of_day = r.nextInt(24) + 0;
int day_of_week = r.nextInt(7) + 0;
Run Code Online (Sandbox Code Playgroud)
速度法:
public static double speed(int time_of_day, int day_of_week) {
double speed = 0;
switch(time_of_day) {
case 0: // 00:00 am
if (day_of_week==0) { //Sun
speed = 24.9;
}else if (day_of_week==1) { // Mon
speed = 26.5;
}else if (day_of_week==2) {// Tues
speed = 26.25;
}else if (day_of_week==3) {// Wed
speed = 27;
}else if (day_of_week==4) {// Thur
speed = 26.25; …Run Code Online (Sandbox Code Playgroud)