Is there anything in Java that can only return the method names within a certain class?

Asked 2 years ago, Updated 2 years ago, 126 views

I create a class with a function and call that class from the menu and name the method in that class in the console window

1.~ 2.~ 3.~

If you choose a number after you make it like this, I want to run the class' method Is there a function like getName? Or should I declare the arrangement for each class and return it?

java method

2022-09-22 22:04

2 Answers

You can use reflection.

Below is a simple example, see link for detailed instructions :)

Input

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class Lambda {
  public void a() { }
  public int b() { return 12; }
  public String c() { return ""; }

  public static void main(String args[]) {
    Class c = Lambda.class;
    Method[] m = c.getDeclaredMethods();

    for (int i = 0; i < m.length; i++) {
      System.out.println(m[i].toString());
    }
  }
}

Output

public static void Lambda.main(java.lang.String[])
public java.lang.String Lambda.c()
public void Lambda.a()
public int Lambda.b()


2022-09-22 22:04

The Object class in Java is a class created by extracting common characteristics of all classes in Java.

The method that this class has is inherited by all other classes in Java, so it's a good idea to know^

Among them, getClass() is a method of finding out the information of the class to which the object belongs. Returns the information of the class to which the object belongs by making it a class-type object.

Class cos = obj.getClass();

Here, the class type is java of the JDK library.The name of the class that belongs to the lang package and functions to contain various information about the class. You can import that information using a method that starts with the name get in this class.

// Rectangle.java
public class Rectangle {
    int width, height;
    Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }
    int getArea() {
        return width * height;
    }
}
// ObjectExample.java
public class ObjectExample {
    public static void main(String[] args) {
        Rectangle obj = new Rectangle(10, 20);
        Class cos = obj.getClass();
        String name = cls.getName(); // Returns the name of the class

        System.out.println ("class name: " + name);

        Class superCls = cls.getSuperClass(); // Return superclass information
        String superName = superCls.getName();

        Field[] = cos.getDeclaredFields(); // Gets the declared field information in the class

        System.out.println ("Field: ");

        for(int cnt = 0; cnt < field.length; cnt++) {
            System.out.println("    " + field[cnt]);
        }

        Method method[] = cls.getDeclaredMethods(); // Gets the method information declared in the class

        System.out.println ("Method: ");

        for(int cnt = 0; cnt < method.length; cnt++) {
            System.out.println("     " + method[cnt]);
        }
    }
}

The execution results are as follows:

Class Name: Rectangle Superclass Name: java.lang.Object Fields: int Rectangle.width int Rectangle.height Methods: int Rectangle.getArea()

To solve the problem you asked, shouldn't you use getDeclaredMethods() of Class to obtain and use the method information?^

^


2022-09-22 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.