在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?

请每个回答只回答一个特征。


当前回答

c++(或Java)中没有封装的事实。任何对象都可以违反任何其他对象的封装,破坏它的私有数据,只要它是相同的类型。例如:

#include <iostream>
using namespace std;

class X
{
  public:
    // Construct by passing internal value
    X (int i) : i (i) {}

    // This breaks encapsulation
    void violate (X & other)
    {
        other.i += i;
    }

    int get () { return i; }

  private:
    int i;
};

int main (int ac, char * av[])
{
    X a(1), b(2), c(3);

    a.violate (c);
    b.violate (c);
    cout << c.get() << endl;    // "6"
}

其他回答

我一直在想为什么最简单的程序是:

class HelloWorldApp {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

然而它可以是:

print "Hello World!"

也许这是为了吓唬计算机科学专业的学生……

在JavaScript中,undefined是一个全局变量,其默认值为原始值undefined。你可以改变undefined的值:

var a = {};
a.b === undefined; // true because property b is not set
undefined = 42;
a.b === undefined; // false

由于undefined的可变性,通过typeof检查undefined通常是一个更好的主意:

var a = {};
typeof a.b == "undefined"; // always true

试试吧,除非,否则

try:     pass
except:  pass
else:    pass
finally: pass

如果没有捕获异常,则执行else部分。

有道理,但一开始我真的不知道它是干什么的。

例子:

def show_square(string):
  try:
    n = int(string, 10)
  except ValueError:
    print "I can't do that, Dave."
  else:
    print n * n

在Common Lisp中,零维数组是很奇怪的,而且很自然地,它们具有读取语法。

? (aref #0A5)
5

PHP对字符串中数值的处理。详见之前对另一个问题的回答,但简而言之:

"01a4" != "001a4"

如果你有两个包含不同数量字符的字符串,它们不能被认为是相等的。前导零很重要,因为它们是字符串而不是数字。

"01e4" == "001e4"

PHP doesn’t like strings. It’s looking for any excuse it can find to treat your values as numbers. Change the hexadecimal characters in those strings slightly and suddenly PHP decides that these aren’t strings any more, they are numbers in scientific notation (PHP doesn’t care that you used quotes) and they are equivalent because leading zeros are ignored for numbers. To reinforce this point you will find that PHP also evaluates "01e4" == "10000" as true because these are numbers with equivalent values. This is documented behaviour, it’s just not very sensible.