Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.*;
- public class FBTest {
- private interface FooI {
- public void foo();
- public void foo2();
- }
- private interface BarI {
- public void bar();
- }
- private class Base {}
- private class Foo extends Base implements FooI {
- public void foo() {
- System.out.println("A");
- }
- public void foo2() {
- System.out.println("1");
- }
- }
- private class FooBar extends Base implements FooI, BarI {
- public void foo() {
- System.out.println("B");
- }
- public void foo2() {
- System.out.println("2");
- }
- public void bar() {
- System.out.println("foobar");
- }
- }
- private class Bar extends Base implements BarI {
- public void bar() {
- System.out.println("bar");
- }
- }
- Base[] arr = {new Foo(), new Bar(), new FooBar()};
- void func(Class ctype, String methodName) {
- for (Base b: arr) {
- if (ctype.isInstance(b)) {
- try {
- Method m = ctype.getDeclaredMethod(methodName);
- m.invoke(b);
- } catch (Exception e) {
- System.out.println("Won't happen in example");
- }
- }
- }
- };
- public static void main(String[] args) {
- FBTest test = new FBTest();
- test.func(BarI.class, "bar");
- test.func(FooI.class, "foo2");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement