C#中var关键字相当于java吗? [英] C# var keyword equivalent in java?
问题描述
一个使用C#中的 VAR 的关键字的是隐式类型声明。什么是Java语法等价的 VAR 的?
有是没有的。唉,你必须键入了完整的类型名称
编辑被张贴,收集一些评论从下面经过6年:
-
原因C#有
VAR
关键字,因为它可能有有没有名字类型。净。例如:VAR MYDATA的=新{A = 1,B =2};
在这种情况下,这将是不可能的,得到适当的类型以
myData的
。 6年前,这在Java中是不可能的(各类有名字,即使他们是非常冗长和unweildy)。我不知道这是否在平均时间已经改变了。 -
VAR
是不一样的动态
。VAR
iables还是静态类型的100%。这不会编译:VAR的myString =富;
的myString = 3;
-
VAR
也时非常有用类型是从上下文显而易见。例如:VAR的currentUser = User.GetCurrent();
我可以说,在我负责的任何代码,
的currentUser
有一个用户
或派生类在里面。很显然,如果你的User.GetCurrent
实施返回一个int,那么也许这是一个有损于你。 -
这无关与
VAR
,但如果你有,你的影子与其他方法的方法怪异继承heirarchies(如新公共无效DoAThing ()
),不要忘记,非虚方法是由它们转换为类型的影响。
我无法想象一个现实世界的场景,这表明良好的设计,但正如你想到这个可能不工作:
类Foo {
公共无效非(){}
公共虚拟无效的Virt(){}
}
类酒吧:富{
公共新空间非(){ }
公共覆盖无效的Virt(){}
}
类巴兹{
公共静态美孚的getFoo(){
返回新的酒吧();
}
}
无功富= Baz.GetFoo();
foo.Non(); //&下; - Foo.Non,不Bar.Non
foo.Virt(); //< - Bar.Virt
VAR栏=(酒吧)富;
bar.Non(); //&下; - Bar.Non,不Foo.Non
bar.Virt(); //&下; - 静止Bar.Virt
正如所指出的,虚拟方法不受此<。 / p>
-
没有,没有初始化
VAR
没有实际的变量非笨拙的方式。VAR foo1 =酒吧; //好的
变种foo2的; //错误的,什么类型的?
VAR foo3 = NULL; //坏,空不具有类型
VAR foo4 =默认值(VAR); //什么?
VAR foo5 =(对象)空; //合法的,但回家了,你醉了
在这种情况下,只是做旧办法:
对象foo6;
One use of the var keyword in C# is implicit type declaration. What is the java equivalent syntax for var?
There is none. Alas, you have to type out the full type name.
Edit 6 years after being posted, to collect some of the comments from below:
The reason C# has the
var
keyword is because it's possible to have Types that have no name in .NET. Eg:var myData = new { a = 1, b = "2" };
In this case, it would be impossible to give a proper type to
myData
. 6 years ago, this was impossible in Java (all Types had names, even if they were extremely verbose and unweildy). I do not know if this has changed in the mean time.var
is not the same asdynamic
.var
iables are still 100% statically typed. This will not compile:var myString = "foo"; myString = 3;
var
is also useful when the type is obvious from context. For example:var currentUser = User.GetCurrent();
I can say that in any code that I am responsible for,
currentUser
has aUser
or derived class in it. Obviously, if your implementation ofUser.GetCurrent
return an int, then maybe this is a detriment to you.This has nothing to do with
var
, but if you have weird inheritance heirarchies where you shadow methods with other methods (egnew public void DoAThing()
), don't forget that non-virtual methods are affected by the Type they are cast as.I can't imagine a real world scenario where this is indicative of good design, but this may not work as you expect:
class Foo { public void Non() {} public virtual void Virt() {} } class Bar : Foo { public new void Non() {} public override void Virt() {} } class Baz { public static Foo GetFoo() { return new Bar(); } } var foo = Baz.GetFoo(); foo.Non(); // <- Foo.Non, not Bar.Non foo.Virt(); // <- Bar.Virt var bar = (Bar)foo; bar.Non(); // <- Bar.Non, not Foo.Non bar.Virt(); // <- Still Bar.Virt
As indicated, virtual methods are not affected by this.
No, there is no non-clumsy way to initialize a
var
without an actual variable.var foo1 = "bar"; //good var foo2; //bad, what type? var foo3 = null; //bad, null doesn't have a type var foo4 = default(var); //what? var foo5 = (object)null; //legal, but go home, you're drunk
In this case, just do it the old fashioned way:
object foo6;
这篇关于C#中var关键字相当于java吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!