要回答您的问题:
这在哪里失败?
- 最终变量和枚举常量
- “特殊”参考,例如
this
- 从方法调用返回的引用,或使用内联构造的引用
new
- 文字(字符串,整数等)
…还有可能是其他人。基本上,
ref仅当参数源是非最终字段或局部变量时,您的关键字才可用。与一起使用时,任何其他来源都应产生编译错误
ref。
(1)的示例:
final String s = "final";passByReference(ref s); // Should not be possible
(2)的示例:
passByReference(ref this); // Definitely impossible
(3)的示例:
passByReference(ref toString()); // Definitely impossiblepassByReference(ref new String("foo")); // Definitely impossible(4)的示例:
passByReference(ref "literal"); // Definitely impossible
然后是赋值表达式,在我看来,这就像一个判断调用:
String s;passByReference(ref (s="initial")); // Possible, but does it make sense?
您的语法
ref在方法定义和方法调用中都需要关键字也有点奇怪。我认为方法定义就足够了。



