在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
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!" ; }
}
其他回答
在找函数吗?为什么不是一门语言呢?
我喜欢PHP,但它总是这样构建:“哦,糟了!我忘了这个!让我们在函数"中添加另一个参数,结果如下:
Str_replace ($search, $replace, $subject…) Strstr ($subject, $search,…)
注意额外的下划线和参数的不同顺序。
这里还有一些东西
$a = array( 'a', 'b', 'c', 'd');
print_r($a); //Prints array( 0 => 'a', 1 => 'b', 2 => 'c', 3 => 'd');
unset($a[2]); //Destroys the element 2 of the list
print_r($a); //Prints array( 0 => 'a', 1 => 'b', 3 => 'd');
Perl文件句柄样式的操作符调用。
一开始是有的
print "foo", "bar", "baz"; # to stdout
print STDERR "foo", "bar", "baz";
注意没有逗号,这样你就知道这是要打印到的文件句柄,而不是要以字符串化方式打印的文件句柄。这是一个肮脏的黑客。
语言升级开始了,他们制作了适当的OO文件句柄,并将x FOO y, z, abc转换为FOO->x(y, z, abc)。有点可爱。相同的print语句有效地运行
STDERR->print("foo", "bar", "baz");
大多数情况下,当你错过一个逗号时,或者尝试运行hashof $a, $b, $c(不带括号的子例程调用)之类的东西时,你会注意到这一点,而忘记从它的实用程序包中将hashof函数导入到你的命名空间中,你会得到一个奇怪的错误消息,关于“无法通过字符串$a的包内容调用方法'hashof'”。
因为我还没见过任何人提起它……RPG 2或3(报告程序生成器…又名火箭推进垃圾)是迄今为止我用过的最疯狂的语言。它几乎没有对程序流的控制(在文件顶部的Enter,在底部的Exit),并且编程语句是基于使用固定字体在特定列中定义的字符定义的(想想PUNCH CARDS!!)
要真正做到FUBAR,你必须尝试用DYL-280编程。它将RPG流程和逻辑与COBOL语法结合在一起。
在这里查找RPG: wikipedia.org /wiki/IBM_RPG
DYL-280的示例:http://99-bottles-of-beer.net/language-dyl-280-224.html
这是我最喜欢的一个,你可以在Java中不使用main()来执行println。
这将编译和运行,给出println,但也有一个异常(java.lang。NoSuchMethodError:主要)
class Test {
static {
System.out.println("I'm printing in Java without main()");
}
}
Perl的许多内置变量:
$# — not a comment! $0, $$, and $? — just like the shell variables by the same name $ˋ, $&, and $' — weird matching variables $" and $, — weird variables for list- and output-field-separators $! — like errno as a number but strerror(errno) as a string $_ — the stealth variable, always used and never seen $#_ — index number of the last subroutine argument... maybe @_ — the (non)names of the current function... maybe $@ — the last-raised exception %:: — the symbol table $:, $^, $~, $-, and $= — something to do with output formats $. and $% — input line number, output page number $/ and $\ — input and output record separators $| — output buffering controller $[ — change your array base from 0-based to 1-based to 42-based: WHEEE! $} — nothing at all, oddly enough! $<, $>, $(, $) — real and effective UIDs and GIDs @ISA — names of current package’s direct superclasses $^T — script start-up time in epoch seconds $^O — current operating system name $^V — what version of Perl this is
还有很多这样的东西。点击这里阅读完整列表。