引入枚举类的一个主要目的,是可以有一种类型安全,不可变的方法来定义常量。在引入枚举类之前,一般通过定义基本型的常量来表达一些固定的值。 比如static final int MONDAY = 1; 这种写法看似没什么问题,可没有什么实际意义的1在这里承担了很重要的职责,不免让人产生疑问,改成0可不可以呢? 而在判断是不是MONDAY时,又必须和1作比较,这就让程序逐渐变得复杂而又危险了。
Class Account intgamma(int inputVal, int quantity, int yearToDate){ int importantValue1 = (inputVal * quantity) + delta(); int importantValue2 = (inputVal * yearToDate) + 100; if ((yearToDate - importantValue1) > 100) importantValue2 -= 20; int importantValue3 = importantValue2 * 7; // and so on. return importantValue3 - 2 * importantValue1; }
voidcheckSecurity(String[] people){ boolean found = false; for (int i = 0; i < people.length; i++) { if (! found) { if (people[i].equals ("Don")){ sendAlert(); found = true; } if (people[i].equals ("John")){ sendAlert(); found = true; } } } }
这段代码使用变量found来判断来逃离for循环,显然可以使用break来代替:
1 2 3 4 5 6 7 8 9 10 11 12
voidcheckSecurity(String[] people){ for (int i = 0; i < people.length; i++) { if (people[i].equals ("Don")){ sendAlert(); break; } if (people[i].equals ("John")){ sendAlert(); break; } } }
voidcheckSecurity(String[] people){ String found = ""; for (int i = 0; i < people.length; i++) { if (found.equals("")) { if (people[i].equals ("Don")){ sendAlert(); found = "Don"; } if (people[i].equals ("John")){ sendAlert(); found = "John"; } } } someLaterCode(found); }
voidcheckSecurity(String[] people){ String found = foundMiscreant(people); someLaterCode(found); } String foundMiscreant(String[] people){ String found = ""; for (int i = 0; i < people.length; i++) { if (found.equals("")) { if (people[i].equals ("Don")){ sendAlert(); found = "Don"; } if (people[i].equals ("John")){ sendAlert(); found = "John"; } } } return found; }
doublegetPayAmount(){ double result; if (_isDead) result = deadAmount(); else { if (_isSeparated) result = separatedAmount(); else { if (_isRetired) result = retiredAmount(); else result = normalPayAmount(); }; } return result; };
doublegetPayAmount(){ if (_isDead) return deadAmount(); if (_isSeparated) return separatedAmount(); if (_isRetired) return retiredAmount(); return normalPayAmount();};
name1 = 'Tom' name2 = 'Jerry' type1 = 'cat' type2 = 'god' print('Hello %s, you are a %s.this is %s, a %s' % (name1, type1, name2, type2)) # 输出结果: 'Hello Tom, you are a cat. this is Jerry, a god'
name1 = 'Tom' name2 = 'Jerry' type1 = 'cat' type2 = 'god' print('Hello {}, you are a {}.this is {}, a {}'.format(name1, type1, name2, type2)) # 输出结果: 'Hello Tom, you are a cat. this is Jerry, a god'
使用format()你还可以自定义参数的插入顺序,例如:
1 2 3 4 5 6
name1 = 'Tom' name2 = 'Jerry' type1 = 'cat' type2 = 'god' print('Hello {0}, you are a {2}.this is {1}, a {3}'.format(name1, name2, type1, type2)) # 输出结果: 'Hello Tom, you are a cat. this is Jerry, a god'
name1 = 'Tom' name2 = 'Jerry' type1 = 'cat' type2 = 'god' print('Hello {name_a}, you are a {type_a}.this is {name_b}, a {type_b}'.format(name_a = name1, name_b = name2, type_a = type1, type_b = type2)) # 输出结果: 'Hello Tom, you are a cat. this is Jerry, a god'
既然可以这样写了,那干脆直接引入dict得了.这个也是可以的:
1 2 3 4 5 6
character = { 'name' : 'Tom', 'type' : 'cat' } print('Hello {name}, you are a {type}.'.format(**character)) # 输出结果: 'Hello Tom, you are a cat.'