How to determine an object's class?

后端 未结 11 1138
醉酒成梦
醉酒成梦 2020-11-22 17:10

If class B and class C extend class A and I have an object of type B or C, how can I determine of which type

11条回答
  •  孤独总比滥情好
    2020-11-22 17:47

    We can use reflection in this case

    objectName.getClass().getName();
    

    Example:-

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        String name = request.getClass().getName();
    }

    In this case you will get name of the class which object pass to HttpServletRequest interface refference variable.

提交回复
热议问题