开发者

Describing Types question

开发者 https://www.devze.com 2022-12-22 00:39 出处:网络
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

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

精彩评论

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