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

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


当前回答

其他奇怪的事情:

在c++中,覆盖一个虚方法会隐藏该方法的所有其他重载。在Java中,这种情况不会发生。这很烦人。例如:http://codepad.org/uhvl1nJp

在c++中,如果基类有一个公共虚方法foo(),子类有一个私有方法foo(),这个私有方法会覆盖另一个方法! 这样,只需将子类对象指针强制转换为父类对象指针,就可以在类外部调用私有方法。这不应该是可能的:这违反了封装。新方法不应被视为对旧方法的重写。例如:http://codepad.org/LUGSNPdh

在PHP中,你可以定义函数来接受类型化参数(例如,对象是某个接口/类的子类),讨厌的是,在这种情况下,你不能使用NULL作为实际的参数值。 例如:http://codepad.org/FphVRZ3S

其他回答

在类C语言(包括C本身)中,你可以使用“向下到”操作符:

for (x = 20; x --> 0;) {
    print x;
}

这将打印从19到0的数字。

在x++ (Microsoft Dynamics AX):

1)需要在单独的行上使用分号(;)将变量声明与语句分开(至少在4.0版本之前)

    int i;
    int myArray[5];
    ;
    i = 1;

2)数组索引是基于1的,所以你不允许像in那样使用索引0(零)从数组中读取

    int myArray[5];
    ;
    print myArray[0];    // runtime error

这并不奇怪,但是你可以在赋值的左边使用0索引,比如in

    int myArray[5];
    ;
    myArray[2] = 102;
    myArray[0] = 100;    // this is strange
    print myArray[2];    // expcting 102?

会发生什么呢?数组被初始化为它的默认值,无论赋值中使用了什么值。上面的代码输出0(零)!

PHP

PHP对实例变量和方法的重载处理不一致。考虑:

class Foo
{
    private $var = 'avalue';

    private function doStuff()
    {
        return "Stuff";
    }

    public function __get($var)
    {
        return $this->$var;
    }

    public function __call($func, array $args = array())
    {
        return call_user_func_array(array($this, $func), $args);
    }
}

$foo = new Foo;
var_dump($foo->var);
var_dump($foo->doStuff());

转储$var是有效的。即使$var是私有的,__get()也会被任何不存在或不可访问的成员调用,并返回正确的值。这不是doStuff()的情况,它失败于:

Fatal error: Call to private method Foo::doStuff() from context ”.”

我认为其中很多都是在c风格的语言中工作的,但我不确定。

Pass a here document as a function argument: function foo($message) { echo $message . "\n"; } foo(<<<EOF Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc blandit sem eleifend libero rhoncus iaculis. Nullam eget nisi at purus vestibulum tristique eu sit amet lorem. EOF ); You can assign a variable in an argument list. foo($message = "Hello"); echo $message; This works because an assignment is an expression which returns the assigned value. It’s the cause of one of the most common C-style bugs, performing an assignment instead of a comparison.

Python

在Python中,可变的默认函数参数会导致意想不到的结果:

def append(thing, collection=[]):
    collection.append(thing)
    return collection

print append("foo")
# -> ['foo']
print append("bar")
# -> ['foo', 'bar']
print append("baz", [])
# -> ['baz']
print append("quux")
# -> ['foo', 'bar', 'quux']

空列表是在函数定义时初始化的,而不是在调用时初始化的,因此对它的任何更改都会在函数调用之间保持不变。

MySQL的大小写敏感性

MySQL有非常不寻常的区分大小写的规则:表区分大小写,列名和字符串值不区分大小写:

mysql> CREATE TEMPORARY TABLE Foo (name varchar(128) NOT NULL);
DESCRIBE foo;
ERROR 1146 (42S02): Table 'foo' doesn't exist
mysql> DESCRIBE Foo;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| name  | varchar(128) | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
1 row in set (0.06 sec)
mysql> INSERT INTO Foo (`name`) VALUES ('bar'), ('baz');
Query OK, 2 row affected (0.05 sec)

mysql> SELECT * FROM Foo WHERE name = 'BAR';
+------+
| name |
+------+
| bar  |
+------+
1 row in set (0.12 sec)

mysql> SELECT * FROM Foo WHERE name = 'bAr';
+------+
| name |
+------+
| bar  |
+------+
1 row in set (0.05 sec)

在PHP中,“true”,“false”和“null”是常量,通常不能被重写。但是,随着PHP >=5.3中名称空间的引入,现在可以在除全局名称空间之外的任何名称空间中重新定义这些常量。这可能导致以下行为:

namespace {
    define('test\true', 42);
    define('test\false', 42);
    define('test\null', 42);
}

namespace test {
    var_dump(true === false && false === null); // is (bool) true
}

当然,如果希望真值为真,总是可以从全局名称空间导入真值

namespace test {
    var_dump(\true === \false); // is (bool) false
}

在Java中,

Int x = 010;

这将x赋值为8。

在Java中,任何前面带0的整数都被假定为八进制。