开发者

Dollar dollar= (Dollar) object; What does this do?

开发者 https://www.devze.com 2023-01-05 23:00 出处:网络
Dollar dollar= (Dollar) object; What does this snippetin do? I开发者_如何学Pythons it even Java? Or Smalltalk? I found it in TDD, which i think was written with Smalltalk in mind.This could be Java.
Dollar dollar= (Dollar) object;

What does this snippet in do? I开发者_如何学Pythons it even Java? Or Smalltalk? I found it in TDD, which i think was written with Smalltalk in mind.


This could be Java. It basically casts an object of generic type (perhaps just Object) to a Dollar object.

Example:

Object object = ObjectFactory.getObject(); // Gets object
Dollar dollar = (Dollar) object; // Cast to Dollar object, will throw an exception
                                 // if this isn't possible
dollar.dollarMethod();   // I can now call Dollar methods


It creates a new variable of class Dollar, with the name dollar. Then it assigns a value to that variable by casting to Dollar a variable named object. It's valid Java code, providing there's a class named Dollar defined. But if the variable named object is not of class Dollar it might throw a ClassCastException.


this actually casts the generic type (Object) to Dollar type.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号