开发者

Convert from Object to Original Class

开发者 https://www.devze.com 2023-01-22 07:05 出处:网络
I\'m storing a instance of Entry class in an Object. Entry newentry = new Entry(j, 0.0); Object test = newentry;

I'm storing a instance of Entry class in an Object.

Entry newentry = new Entry(j, 0.0);
Object test = newentry;

How can I convert the test Object back into an Entry 开发者_StackOverflow社区class to access the Entry class method getValue()?


Typecast it:

Entry newentry = new Entry(j, 0.0);
Entry test = (Entry) newentry;

Also, if getValue is a class method, you don't need an instance to access it, you can call it directly:

Object x = Entry.getValue();


First answer:

Change the datatype of test from Object to Entry in your example.

Second answer:

If you really need to assign it to an object (in this case, a variable called "test") somewhere along the way (for some reason) then you can do this to get it back:

Object val = ((Entry)test).getValue();
0

精彩评论

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