在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在我看来,这在c++中是不允许的:
class A {
public:
virtual string foo(){return "A::foo";}
};
class B : public A {
public:
virtual string foo(){return "B::foo";}
};
int main () {
B* b = new B();
// In my opinion the following should not be allowed
cout << b->A::foo() << endl; // Will print "A::foo"
}
这似乎是正确的,但这意味着如果不允许子类的用户调用原始方法而不是新方法,就不能重写方法。
只要考虑一个集合的子类,当向集合本身添加一个元素时,您希望增加元素的数量。
一个合乎逻辑的解决方案是重写add()方法,在添加元素之前增加计数器,但是新集合的用户可以使用旧方法向其添加元素,这样就绕过了增量,导致元素计数器与集合的实际元素数量不一致。
这在Java中是不可能的。
其他回答
在c++中,你可以做:
std::string my_str;
std::string my_str_concat = my_str + "foo";
但你不能:
std::string my_str_concat = "foo" + my_str;
操作符重载通常服从WTF。
A very tiny thing that annoyed me in COBOL was that there was no dedicated modulo operation. Instead you could do a division specifying that you only wanted whole number results and store the rest in a different variable. Since COBOL is very sensitive when it comes to variables that means that you ended up with a variable you didn't really need, i.e. the actual result of the division. This is the story of how I once named a variable "USELESS" - that was the most appropriate name I could think of.
这缺少了一个奇怪的特性:Python没有switch语句(尽管存在变通方法)。
在ruby/python/c中,你可以像这样连接字符串:
a = "foo" "bar"
print a # => "foobar"
Python for循环中的else。
来自Python文档:
for n in range(2, 10):
for x in range(2, n):
if n % x == 0:
print n, 'equals', x, '*', n/x
break
else:
# loop fell through without finding a factor
print n, 'is a prime number'
输出:
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3