简体   繁体   English

理解java中的“new”关键字

[英]Understanding “new” keyword in java

I would like to know if somebody knows exactly what happens when the line of code comes to play: 我想知道是否有人确切知道代码行发生时会发生什么:

class Class{}

class Math extends Class{}

class UseClasses{
public static void main (String[]args)

{

   new Math();  //Line 8
   (Class)new Math();//     Line 9

}

I thoroughly understand that the "new keyword" serves as object instance creator in the heap memory. 我完全理解“new keyword”在堆内存中充当对象实例创建者。 However in the preceding bit of code you can see that line 9 makes an unusual use of this keyword (new). 但是,在前面的代码中,您可以看到第9行对此关键字(新)的使用不正常。 Line 8 it's ok and compiles fine. 第8行没关系并且编译得很好。 but line 9 requires to assign the content to some other references. 但是第9行需要将内容分配给其他一些引用。 Thus this implies that each time the cast operand is present, in this case (Class)new Math, a new reference(underscoring reference and not object) is going to be instantiated. 因此,这意味着每次存在强制转换操作数时,在这种情况下(Class)new Math,将实例化新的引用(强调引用而不是对象)。

Does it work in this way? 它以这种方式工作吗? If not, could you explain to me why it compiles fine in line #8 and it gives an error in line #9? 如果没有,你能解释一下为什么它在第8行编译得很好并且它在第9行中出错了吗? (obviously due to the casting function not to be put there but why not?) (显然由于铸造功能不在那里,但为什么不呢?)

Since you don't have referencing variable on line 9 compiler doesn't consider this statement to be valid. 由于第9行没有引用变量,编译器不认为此语句有效。

Cast is required when you have a referencing variable of a 'more specific' type that the object itself. 如果您具有对象本身的“更具体”类型的引用变量,则需要强制转换。 For example, when you're performing an upcast you don't need it: 例如,当您执行上传时,您不需要它:

Class obj = new Math();

And you need an explicit cast when you're performing downcast: 当你执行向下转换时,你需要一个明确的演员:

Class obj = new Math();
Math math = (Math) obj;

(Class)new Math() does two things: (Class)new Math()做两件事:

  1. create a new instance of Math 创建一个新的Math实例
  2. cast the created Math instance to Class. 将创建的Math实例强制转换为Class。

Since Math extends Class, every Math instance is a Class instance, and you don't need any cast to assign a Math instance to a variable of type Class. 由于Math扩展了Class,因此每个Math实例都是一个Class实例,并且您不需要任何强制转换来将Math实例分配给Class类型的变量。 The cast is thus completely useless. 因此,演员阵容完全没用。 And since you don't assign the created Math instance to any variable, the cast is even more useless. 由于您没有将创建的Math实例分配给任何变量,因此强制转换更无用。

Both lines build a Math instance. 这两行都构建了一个Math实例。 Since in this case, Math is a subclass of Class (which is kinda unusual, but okay, I'm playing along). 因为在这种情况下, MathClass的子Class (这有点不寻常,但好吧,我正在玩)。

So line 9, (Class)new Math(); 所以第9行, (Class)new Math(); tells the JVM to forget about the fact it's a specialized instance of Class and just treat it like a generic Class object. 告诉JVM忘记它是Class的一个专门实例,只是把它当成一个普通的Class对象。 That basically means that if you did: 这基本上意味着,如果你这样做:

Class c = (Class)new Math(); and Math had methods not inherited from Class , you wouldn't be able to call them on object c . Math没有从Class继承的方法,你将无法在对象c上调用它们。

Note however that c is still actually an instance of Math . 但请注意, c实际上仍然是Math一个实例。

I'm not sure really what the point is in this example, but typically you do this if you want your code work off of the generic definition of an object and not worry about specific implementations. 我不确定这个示例中的重点是什么,但是如果您希望代码在对象的泛型定义之外工作而不担心特定的实现,通常会这样做。 I don't know, the whole example seems goofy to me. 我不知道,整个例子对我来说似乎很傻。

在这种特殊情况下,这只是因为没有对第9行的变量进行赋值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM