在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
在c#中,这至少应该生成一个编译器警告,但它没有:
public int Something
{
get { return Something; }
set { Something = value; }
}
当被调用时,它会导致你的应用程序崩溃,你不会得到一个好的堆栈跟踪,因为它是一个StackOverflowException。
其他回答
在Perl中,可以通过修改一个类的@ISA数组来改变它的继承链,这让我很惊讶。
package Employee;
our @ISA = qw(Person);
# somwhere far far away in a package long ago
@Employee::ISA = qw(Shape);
# Now all Employee objects no longer inherit from 'Person' but from 'Shape'
Python的三元操作符
在Python中,C三元操作符(c++示例:bool isNegative = i < 0 ?True: false;)可用作语法糖:
>>> i = 1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is positive'
>>> i = -1
>>> "It is positive" if i >= 0 else "It is negative!"
'It is negative!'
这并不奇怪,而是一种特征。奇怪的是,与C中的顺序(条件?答:b)。
C的多个名称空间:
typedef int i;
void foo()
{
struct i {i i;} i;
i: i.i = 3;
printf( "%i\n", i.i);
}
或与字符:
typedef char c;
void foo()
{
struct c {c c;} c;
c: c.c = 'c';
printf( "%c\n", c.c);
}
Perl充满了奇怪但整洁的特性。
If可以用在语句之前或之后,就像这样:
print "Hello World" if $a > 1;
if ($a > 1) { print "Hello World"; }
foreach也是如此:
print "Hello $_!\n" foreach qw(world Dolly nurse);
c#的默认继承模型赢得了我的投票:
public class Animal
{
public string Speak() { return "unknown sound" ; }
}
public class Dog : Animal
{
public string Speak() { return "Woof!" ; }
}
class Program
{
static void Main( string[] args )
{
Dog aDog = new Dog() ;
Animal anAnimal = (Animal) aDog ;
Console.WriteLine( "Dog sez '{0}'" , aDog.Speak() ) ;
Console.WriteLine( "Animal sez '{0}'" , anAnimal.Speak() ) ;
return ;
}
}
运行程序得到如下结果:
狗叫“汪!” 动物说“未知的声音”
获得这种行为应该要求程序员走出程序员的道路。子类实例不会因为被上转换为它的超类型而停止存在。相反,你必须显式地请求预期的(几乎总是想要的)结果:
public class Animal
{
public virtual string Speak() { return "unknown sound" ; }
}
public class Dog : Animal
{
public override string Speak() { return "Woof!" ; }
}