Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
117 views
in Technique[技术] by (71.8m points)

java - Convert from Object to Original Class

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 class to access the Entry class method getValue()?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...