challenge メソッド名一覧の表示

リフレクション系のお題の続編です。

「ある与えられたオブジェクトtargetのメソッドのうち、 "test_"で始まるものをすべて呼びだす」というコードを書いてください。 引数に関しては都合のいいように仮定して構いません(全部0個、など)。

メソッドという概念がない言語の場合は、 「複数の関数への参照を持っているようなオブジェクト(たとえばパッケージとかモジュールとか)から"test_"で始まる関数をすべて呼び出す」と読み替えても構いません。

Posted feedbacks - JavaFxScript

[obj].class.Operations で、[obj]のクラスのメソッド一覧を取得できます。 上記で取得できるのはOperationクラスで、 Operation.Name でオペレーション名を取得できます。

念のためoperation, functionの両方で試しましたが、 両方ともOperationsで取得できました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import java.lang.System;

public class TestClass {
    public function test_function();
    public operation test_operation();
    public function notest_function();
    public operation notest_operation();
}

var a = new TestClass();

var testNames = select op.Name from op in a.class.Operations 
    where op.Name.startsWith("test_");

for(name in testNames) {
    System.out.println(name);
}

Index

Feed

Other

Link

Pathtraq

loading...