2012-02-01 111 views
10

我在Java中,以下问题得到了类继承的类: 我有一个基类和派生类中,我有在基类中的方法。当我通过Derived调用Base的foo方法时,我想获得Derived的类。如果可以这样做,foo方法可以是通用的。的Java:从静态方法

class Base 
{ 
    static void foo() 
    { 
     // I want to get Derived class here 
     // Derived.class 
    } 
} 

class Derived extends Base 
{ 
} 

Derived.foo(); 

感谢您的帮助!

David

+0

我不确定这是否可行,而不是像你这样做的静态方法。 – 2012-02-01 00:27:26

+2

“我想得到Derived的课程”,这似乎是一件很奇怪的事情。是什么原因?可能有更好的方法来解决你的真实问题 – Raedwald 2012-02-01 00:27:56

+0

我有一个叫做ActiveModel的模型,我想要一个类似于Rails的界面,例如:列表 brands = Brand.all();品牌从ActiveModel延伸(ActiveModel具有全部功能),现在我必须将Brand.class传递给all函数才能为我的resoruce生成一个url,例如:http://localhost/brands.json我的目标从列表中删除Brand.class 品牌= Brand.all(Brand.class) – seriakillaz 2012-02-01 00:28:50

回答

4

这不是静态方法的工作方式。你必须实现Derived.foo(),做什么,那就是很特别的Derived,然后将该方法调用Base.foo()。如果您确实需要类型信息,则可以创建Base.foo0(Class klass)

但说实话,这需要知道类型,它的上或许应该是一个实例方法调用的类的任何静态方法。

+0

是的,其实这是我第一个解决这个问题的方式 – seriakillaz 2012-02-01 00:34:07

0

不可能的,Derived.foo()会简单地给代码Base.foo()

3

嗯,Derived.foo()调用者知道他们在呼唤什么,所以你可以从而改变你的方法:

class Base 
{ 
    static void foo(Class<T> calledBy) 
    { 
     // I want to get Derived class here 
     // Derived.class 
    } 
} 

class Derived extends Base 
{ 
} 

Derived.foo(Derived.class); 
1

static方法不是在继承。具有相同签名的静态方法只能隐藏超类中的类似方法。这意味着你永远不会看到你可能想要的结果 - 你总是完全知道封闭类。静态方法永远不可能在另一个类中“内部”。所以产生期望的结果是不可能的。从调用子类或实例的静态方法就是这个原因,因为它只是隐藏的真正类是一个坏主意。 (IDE和静态代码分析工具可以标记或更正。)

来源:

那么与继承方法的工作原理不static方法的工作没有继承。

class Base { 
    static void foo() { 
     // Only the static context is available here so you can't get class dynamic class information 
    } 
    void bar() { 
     System.out.println(getClass()); 
    } 
} 
class Derived extends Base { 
} 
class Another extends Base { 
    static void foo() { 
     // No super call possible! 
     // This method hides the static method in the super class, it does not override it. 
    } 
    void bar() { 
     super.bar(); 
    } 
} 

Derived derived = new Derived(); 
derived.bar(); // "class Derived" 
Base base = new Base(); 
base.bar(); // "class Base" 

// These are only "shortcuts" for Base.foo() that all work... 
derived.foo(); // non-static context 
Derived.foo(); // could be hidden by a method with same signature in Derived 
base.foo(); // non-static context 

Base.foo(); // Correct way to call the method 

Another a = new Another(); 
a.foo(); // non-static context 
Another.foo(); 

语言允许这样吗? - 嗯。我认为它告诉IDE和代码分析工具警告,甚至可以自动纠正这一点。

-1
Derived.foo(); 

转到Derived定义foo,如果定义有:

class Base { 
    static void foo() { 
    System.out.println("Base"); 
    } 
} 

class Der extends Base { 
    static void foo() { 
    System.out.println("Der"); 
    } 
} 

class Check { 
    public static void main(String[] args) { 
    Base.foo(); 
    Der.foo(); 
    } 
} 

当我运行它:

javac -g Check.java && java Check 
Base 
Der 

那么,什么是你的问题?如果要求每一个派生类implement foo这是不可能在Java执行。

+0

问题是:没有'Derived.foo'但仍然会在'foo'中将'Derived'作为类名,就像非静态方法一样。 – 2012-02-01 01:28:08

+0

你能举个例子吗?我不这么认为... – 2012-02-01 07:15:01

+0

我?对不起,我不认为我得到你的问题。问题中的例子显示了OP想要什么,但这是不可能的。 – 2012-02-01 08:18:23