2013-12-16 25 views
0

我有一个函数,首先检查所传递的参数类型:处理不同类型的对象在一个通用的方式

public void myFunc(Object myObj){ 
    if(myObj instanceof Student){ 
     Student p = (Student) myObj; 
    }else{ 
     Teacher p = (Teacher) myObj; 
    } 

    //after above check I want to handle p in a generic way 
    //of course the following p is not resolved... 
    p.getName(); 
    p.registerSelf(); 
} 

p总是需要先投。如何使编译器首先知道p的类型,然后调用Teacher & Student这两个共同函数。

我的人和老师是通过使用apache avro自动生成的。我无法定义两个类(Person &学生)来扩展同一个类。

+0

那么,不要发表同样的问题两次。至少没有任何改善。为什么你用[tag:performance]标签标记这个?这是无关的。 –

回答

1

有学生和老师从一个共同的父母 - 延长人。 将方法getName和registerSelf置于Person中。然后无条件地将Person输入到Person中,并调用您在Person中输入的常用方法。

行,“我的人和老师是通过使用apache avro自动生成的,我无法定义两个类(Person & Student)来扩展相同的类。

这部分是不是原来的问题,我认为。

基于新的信息,我会说:

1)我有一个生成的类类似的问题在我的项目之一。我不认为有一个很好的解决方案,除非你在生成这些类后强制学生和教师实现一些通用接口(并且不需要手动更改生成的类/我想你会希望你可能需要这样的需求他们在一些版本控制系统中/)。我建议你发布一个新问题,因为重要的声明不是其初始版本的一部分。

2)你也可能想看看http://en.wikipedia.org/wiki/Decorator_pattern不知道它是否适用于你的情况,但你检查出来,也许它是。

3)您可以使用grexter89指出的反射。这对你的情况可能是最好的。

+0

我的人和老师是通过使用apache avro自动生成的。我无法做到这一点。 –

+0

@ Leem.fin啊,你应该提到 –

+0

感谢您的有用建议 –

0

首先,你的代码不能编译。

其次:

public interface Person{ 
    String getName(); 
    void registerSelf(); 
} 

public class Student implements Person{ 
    .... 
} 

public class Teacher implements Person{ 
    .... 
} 

和你的代码更改

public void myFunc(Object myObj){ 
    Person p = null; 
    if(myObj instanceof Student){ 
     p = (Student) myObj; 
    }else{ 
     p = (Teacher) myObj; 
    } 

    //after above check I want to handle p in a generic way 
    //of course the following p is not resolved... 
    p.getName(); 
    p.registerSelf(); 
} 
+0

为什么你会'如果instanceof',将其转换为特定类型然后将其分配给更一般的接口? –

0

如果Teacher & Student有你想打电话,请创建这些方法接口(或超类)相似的方法,然后用myFunc(ParentClass myObj)并且不需要任何东西:

public void myFunc(ParentClass myObj){ 
    myObj.getName(); 
    myObj.registerSelf(); 
} 
0

Student pTeacher p有范围,直到if条款。所以首先申报Person p = null

而且,如果你想与每个人做同样的事情,那么为什么使用instanceof。简单地将该方法提取到超类,然后在子类中覆盖它们。Beware of instanceof

0

如果你真的没有办法强制Interface,看看Reflection

Here's an example on how to use it

+0

我认为使用反射不适合维护,想象如果我改变了教师和学生类我不会得到编译器错误,但只有运行时错误 –

0

假设你不能让从一个接口扩展类,你必须使用某种反思。一个可能的解决方案可能如下:

public void myFunc(Object myObj) { 
    if (myObj instanceof Student) { 
     Student p = (Student) myObj; 
    } else { 
     Teacher p = (Teacher) myObj; 
    } 

    //after above check I want to handle p in a generic way 
    //of course the following p is not resolved... 

    // define possible classes for invoking methods 
    List<Class<?>> possibleClasses = new ArrayList<Class<?>>() {{ 
     add(Student.class); 
     add(Teacher.class); 
     add(NewUnknown.class); // just in case for more possibilities 
    }}; 

    if (possibleClasses.contains(myObj.getClass())) { 
     try { 
      // directly invoking these methods, assuming, 
      // they exist in the defined classes 
      myObj.getClass().getMethod("getName").invoke(myObj); 
      myObj.getClass().getMethod("registerSelf").invoke(myObj); 
     } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { 
      // handle exception, if method is not available in current class 
     } 
    } 
} 
相关问题