[JavaSE] basic reflection-Class operations, javase-class

Source: Internet
Author: User

[JavaSE] basic reflection-Class operations, javase-class

Get the class name

How to obtain this class

Return Value Type of the method.

Get method name

Type of parameters for Obtaining Methods

Package com. tsh. reflect; import java. lang. reflect. method; public class ReflectDemo {public static void main (String [] args) {// int Class class c1 = int. class; // String Class type. The Byte Code of String is class c2 = String. class; Class c3 = double. class; Class c4 = void. class; System. out. println (c1.getName (); // output int System. out. println (c2.getName (); // output java. lang. string ReflectDemo reflectDemo = new ReflectDemo (); printClass (refle CtDemo);}/*** print Class information * @ param obj */public static void printClass (Object obj) {Class c = obj. getClass (); System. out. println ("Class Name:" + c. getName (); // obtain the public Method of the class. Method [] methods = c. getMethods (); for (Method method: methods) {// obtain the returned value type Class returnTypeClass = method. getReturnType (); String returnType = returnTypeClass. getSimpleName (); // obtain the method name String methodName = method. getName (); // obtain the parameter type. Ss [] paramClass = method. getParameterTypes (); String params = new String (); int I = 0; for (Class param: paramClass) {if (I! = 0) {params + = "," + param. getSimpleName ();} else {params + = param. getSimpleName ();} I ++;} System. out. println (returnType + "" + methodName + "(" + params + ")");}}}

Returned results

Class Name: com. tsh. reflect. reflectDemovoid main (String []) void printClass (Object) void wait (long, int) void wait (long) void wait () boolean equals (Object)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.