Method

์ƒ˜ํ”Œ ์ฝ”๋“œ ์ž‘์„ฑ

public class Sample {
    private String data1;
    private String data2;

    public Sample() {
    }

    public Sample(String data1, String data2) {
        this.data1 = data1;
        this.data2 = data2;
    }
    public String data1() {
        return data1;
    }
    public String data1(String raw) {
        return data1 + raw;
    }
}

Method

Class<Sample> clazz = Sample.class;

// ์ „์ฒด ์กฐํšŒ
Method[] methods = clazz.getMethods();

// ๋‹จ์ผ ์กฐํšŒ
// Method data1 = clazz.getDeclaredMethod("data1");
// Method data2 = clazz.getDeclaredMethod("data1", String.class);
Method data1 = clazz.getMethod("data1");
Method data2 = clazz.getMethod("data1", String.class); ...

method ์‹คํ–‰

Sample sample = new Sample("1", "2");
Class<? extends Sample> clazz = sample.getClass();

// ์ธ์ž X
Method data1 = clazz.getMethod("data1");
Object invoke = data1.invoke(sample);

// ์ธ์ž O
Method data2 = clazz.getMethod("data3", String.class);
Object invoke1 = data2.invoke(sample, "3");

Last updated