父类中有个static方法,用子类类名调用这个static方法,怎么判断是哪个子类调的这个方法?

解决方案 »

  1.   


    子类调用父类的static方法,static方法里边不能用this关键字
      

  2.   

    话说你想知道那个子类调用这个方法 那这个方法还能被认为是static的吗 有意义吗?
      

  3.   

    我不确定有没有一种方法能获得,子类信息貌似被遮蔽,因为静态方法只存在于父类
    public class Test {
    static void hello() {
    System.out.println("hello"); StackTraceElement stack[] = (new Throwable()).getStackTrace();
    int ix = 0;
    while (ix < stack.length) {
    StackTraceElement frame = stack[ix];
    System.out.println(frame);
    ix++;
    }
    } public static void main(String args[]) {
    A.hello();
    }}class A extends Test {}class B extends Test {}
    hello
    Test.hello(Test.java:6)
    Test.main(Test.java:18)
      

  4.   

    那Spring的AOP能不能拦截static方法啊?
      

  5.   

    instance of 你判断是哪个类就是哪个类了
      

  6.   

    instance of 后面跟什么啊? 用不了this关键字,没有引用啊。
      

  7.   

    向静态方法传递一个子类实例,通过实例.getClass()来判断具体子类类型
      

  8.   


    这么写每次都传了个多余的参数啊,好像只能这么实现了,传子类的Class对象就行了。
      

  9.   

    package com.xuz.csdn.july07;public class StaticClassTest {
    public static void hello(Class<? extends StaticClassTest> c){
    System.out.println(c.getName());
    }
    }class SubA extends StaticClassTest{
    public static void main(String[] args){
    StaticClassTest.hello(SubA.class);
    }
    }
      

  10.   

     StaticClassTest.hello(SubA.class);
    改成
     SubA.hello(SubA.class);
      

  11.   

    java语法本身没有提供这种机制。。要实现的话只能多传一个没用的参数。
      

  12.   


    boolean isAssignableFrom(Class<?> cls) 
              判定此 Class 对象所表示的类或接口与指定的 Class 参数所表示的类或接口是否相同,或是否是其超类或超接口。 
      

  13.   

    不知道楼主具体要做什么,不过Class的isAssignableFrom方法应该能满足。
      

  14.   

    把当前类的实例作为参数传递给方法,这个方法可行。否则请修改设计!
    这个用法不太符合OO惯例。static的东西和继承没有多大关系,要使用继承特性时,不要把static搞进来,这只会增加迷惑性。
      

  15.   

    向静态方法传递一个子类实例,通过实例.getClass()来判断具体子类类型
      

  16.   

    把this.getClass().getName()当参数传递到父类的static方法中。