I have a bunch of types (eg. LargePlane, SmallPlane) that could be in this collection i've made, how do i print like LargePlane? I've tried like typeOf() and stuff but it doesn't work. Within like a toString()? So when i output the 开发者_StackOverflow社区collection it states what type it is.
Use .getClass().getName()
The following example uses a Class object to print the class name of an object:
void printClassName(Object obj)
{
System.out.println("The class of " + obj +
" is " + obj.getClass().getName());
}
For more details, take a look at JavaDoc for Class.
You could either implement an interface or extend something like a "Plane" class
public class LargePlane implents IPlane {
public String toString() {
// build return string here
}
}
public interface IPlane {
public String toString();
}
精彩评论