在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“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"
}
其他回答
在Java中
byte b = 0;
b++;
等于
byte b = 0;
b = b + 1;
但事实并非如此。实际上,你会得到一个编译器错误,因为加法的结果是int类型的,因此不能赋值给字节变量b。当使用复合运算符时,编译器会自动在这里插入一个类型转换。所以
b++;
就变成了
b = (byte) b + 1;
通知7。一个有效程序的例子:
Chomsky is a room. A thought is a kind of thing. Color is a kind of value. The colors are red, green and blue. A thought has a color. It is usually Green. A thought can be colorful or colorless. It is usually colorless. An idea is a thought in Chomsky with description "Colorless green ideas sleep furiously." A manner is a kind of thing. Furiously is a manner. Sleeping relates one thought to one manner. The verb to sleep (he sleeps, they sleep, he slept, it is slept, he is sleeping) implies the sleeping relation. Colorless green ideas sleep furiously.
像图灵机模拟器这样的其他愚蠢的东西可以找到。
在Ruby中…
i=true
while(i)
i=false
a=2
end
puts defined?(a) // returns true
在c#中: A = cond ?B: c; 如果“b”和“c”是“赋值不兼容”,你永远不会得到结果,即使“a”是对象。 它是ms中最常用和最愚蠢的实现运算符。有关比较,请参阅D语言中的实现(关于类型推断的注意)。
在c++中,你可以从空指针调用静态方法——看!
class Foo {
public:
static void bar() {
std::cout << "WTF!?" << std::endl;
}
};
int main(void) {
Foo * foo = NULL;
foo->bar(); //=> WTF!?
return 0; // Ok!
}
这句话让我很意外……