How do I pass a class as a parameter in Java?

后端 未结 10 1154
遥遥无期
遥遥无期 2020-12-04 08:30

Is there any way to pass class as a parameter in Java and fire some methods from that class?

void main()
{
    callClass(that.class)
}

void callClass(???? c         


        
10条回答
  •  情话喂你
    2020-12-04 09:23

    public void callingMethod(Class neededClass) {
        //Cast the class to the class you need
        //and call your method in the class
        ((ClassBeingCalled)neededClass).methodOfClass();
    }
    

    To call the method, you call it this way:

    callingMethod(ClassBeingCalled.class);
    

提交回复
热议问题