public class test{
public static void main(String[] args){
A a=new A();
B.go(a);
}
}
//方法类
class B{
public static void  go(Object C){
//问题是在此处如何将参数C的类型还原成为子类型A
//不可以直接使用强制转换(A)C的方法
//主要是解决为这种方法类的静态方法传入的对象后,使用对象的方法,这样就必须
//还原引入对象的原来类型
//???????????但是有没有人可以告诉我这里该怎么解决呢??????
//?????????这类该怎么进行转换呢???????????????????????????????
}
}//外类
class A{
private String x="hello";
public void getX(){
System.out.println(x);
}
}

解决方案 »

  1.   

    使用RTTI的方法。虽然不能还原成为原来的类型,但是可以使用所有的public方法
      

  2.   

    import java.lang.reflect.*;public class ShowMethods {
        public static void main(String[] args) {
            Class c = Class.forName("A");   //参数是类的名字
            Method[] m = c.getMethods();
            for (int i = 0; i < m.length; i++) {
                System.out.println(m[i]);
            }
        }
    }
      

  3.   

    用这个吧,前提是你应该知道类的名字
    boolean isInstance(Object obj) 
              Determines if the specified Object is assignment-compatible with the object represented by this Class.
      

  4.   

    instanceof 方法不可以用,因为类的名字是在运行中动态出现的!
    TO: TalentXu(义薄云天) ,你说的方法我太清楚:
    1>得到METHOD对象后,怎么执行这个方法,比如A类的getX()方法
    2>这样是否还能访问传入参数对象的属性对象
      

  5.   

    Method有个方法叫invoke
    public Object invoke(Object obj,
                         Object... args)
                  throws IllegalAccessException,
                         IllegalArgumentException,
                         InvocationTargetException
    Invokes the underlying method represented by this Method object, on the specified object with the specified parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary. 
    If the underlying method is static, then the specified obj argument is ignored. It may be null. If the number of formal parameters required by the underlying method is 0, the supplied args array may be of length 0 or null. If the underlying method is an instance method, it is invoked using dynamic method lookup as documented in The Java Language Specification, Second Edition, section 15.12.4.4; in particular, overriding based on the runtime type of the target object will occur. If the underlying method is static, the class that declared the method is initialized if it has not already been initialized. If the method completes normally, the value it returns is returned to the caller of invoke; if the value has a primitive type, it is first appropriately wrapped in an object. However, if the value has the type of an array of a primitive type, the elements of the array are not wrapped in objects; in other words, an array of primitive type is returned. If the underlying method return type is void, the invocation returns null. 
    Parameters:
    obj - the object the underlying method is invoked from
    args - the arguments used for the method call 
    Returns:
    the result of dispatching the method represented by this object on obj with parameters args 
    Throws: 
    IllegalAccessException - if this Method object enforces Java language access control and the underlying method is inaccessible. 
    IllegalArgumentException - if the method is an instance method and the specified object argument is not an instance of the class or interface declaring the underlying method (or of a subclass or implementor thereof); if the number of actual and formal parameters differ; if an unwrapping conversion for primitive arguments fails; or if, after possible unwrapping, a parameter value cannot be converted to the corresponding formal parameter type by a method invocation conversion. 
    InvocationTargetException - if the underlying method throws an exception. 
    NullPointerException - if the specified object is null and the method is an instance method. 
    ExceptionInInitializerError - if the initialization provoked by this method fails.
      

  6.   

    这里可用这样
    m[0].invoke(C);
      

  7.   

    对象属性和构造还属都能访问,只要都是public
      

  8.   

    楼上的朋友们,我给出的例子只是为了为你让大家更好的解决出现的这种问题
    这里静态方法是为了给BEAN中的资料进行自动填充,由于bean 的类型不确定,
    所以只能以OBJECT对象引用参数,但在方法中必须对该bean的对象类型进行还原,
    这样才能使用他里面的PUBLIC 方法
    着需要在运行中动态判断传入参数的原来类型,着并不困难,主要问题是如何解决掉
    类型还原问题楼上的,不可以直接使用强制转换(A)C的方法,因为穿入的可能是B,或者C或者连你都不知道的类型,所以需要系统自动判定并自动转换!着才识问题的关键,.,.,,,
    我等待大家能够提出更好的解决建议........................................................... 
      

  9.   

    楼主,你想重新发明轮子的话我没有意见,以下就请省略不看apache有一个common包叫beanutils,解决了这个问题
    http://jakarta.apache.org/commons/beanutils/简单说,就是你给他一个class,他找出所有get/set方法并以同名字段进行填充。类型无所谓,反正运行时也是取到Object类型,直接传进去。也就是说类型安全是由程序员负责建议你经常去apache
      

  10.   

    说实话,你完全可以在你的方法中要求调用者传给你对象的类名称,比如c.getClass()
      

  11.   

    TO:allenhe
      我昨天就再看beanutils,就是没有看懂,里面调来雕去的,
      你们谁知道是那一段,帮忙给我帖上来,不胜感激!上面的问题好再已经找到解决答案了,再此感谢大家的建议!现在公布出来,
    这个东西也是具体应用在SERVLET上自动读取表单元素的!
    *****这是一个实验品,该方案主要用在SERVLET中自动将表单数据存放在类似一个BEAN的自定义类中,******/
    import java.lang.reflect.Method;
    public class test{
        public static void main(String[] args)throws Exception{
            A a=new A();
            B.go(a);
            }
        
        }
    //方法类
    class B{
        //选择执行传入对象的public方法的演示
             //*****这里的形参C可以使用任何自定义的类,只要它的public方法以get开始
             
        public static void  go(Object C) throws Exception{        Method[] mothod=C.getClass().getMethods();
        for(int i=0;i<mothod.length;i++){
            String str=mothod[i].getName();
            str=str.substring(3,str.length());
            System.out.println("i="+i+",getName="+str);
            if(str.equals("X"))
                mothod[i].invoke(C,"你好");
            
            if(str.equals("Y"))
                mothod[i].invoke(C,"java");
            }
            }
        }//外类    
    class A{
        private String x="hello";
        public void getX(String y){
            System.out.println(x+":"+y);
            }
        public void getY(String a){
            System.out.println("this is :"+a);
            }
        }