简体   繁体   English

从Java调用继承的类方法

[英]Calling An Inherited Class Method From Java

In Python, class methods can be inherited. 在Python中,类方法可以被继承。 eg 例如

>>> class A:
...  @classmethod
...  def main(cls):
...   return cls()
...
>>> class B(A): pass
...
>>> b=B.main()
>>> b
<__main__.B instance at 0x00A6FA58>

How would you do the equivalent in Java? 您将如何在Java中做等效的事情? I currently have: 我目前有:

public class A{
    public void show(){
        System.out.println("A");
    }
    public void run(){
        show();
    }
    public static void main( String[] arg ) {
        new A().run();
    }
}
public class B extends A{
    @Override
    public void show(){
        System.out.println("B");
    }
}

I'd like to call B.main() and have it print "B", but clearly it will print "A" instead, since "new A()" is hardcoded. 我想调用B.main()并将其打印为“ B”,但显然它将打印为“ A”,因为“ new A()”是硬编码的。

How would you change "new A()" so that it's parameterized to use the class it's in when called, and not the hard-coded class A? 您将如何更改“ new A()”,以便对其进行参数化以使用被调用时所在的类,而不是硬编码的类A?

您的B类没有main方法,并且static方法也不被继承。

The only way I can see this happening is to find whatever is calling A.main( String[] arg ) and change it to call B.main instead. 我可以看到这种情况的唯一方法是查找正在调用A.main( String[] arg )然后将其更改为调用B.main

B.main: B.main:

   public static void main( String[] arg ) {
        new B().run();
    }

How is your program started? 您的程序如何启动? Is there a batch file, shortcut, etc? 是否有批处理文件,快捷方式等? Something you can change? 有什么可以改变的吗? Where does A.main get called? A.main在哪里打电话?

Static methods in java are not classmethod s they are staticmethod s. Java中的静态方法不是classmethod它们是staticmethod In general it is not possible to know which class reference the static method was called from. 通常,不可能知道从哪个类引用了静态方法。

I think this isn't possible. 我认为这是不可能的。 Here's why: 原因如下:

In Java, the implementation of a method is determined by the instance's run-time type. 在Java中,方法的实现由实例的运行时类型确定。 So, to execute B.show() , you need to have an instance of B . 因此,要执行B.show() ,您需要具有B的实例。 The only way I could see to do this, if the method that constructs the instance is supposed to be inherited, is to use Class.newInstance() to construct an instance of a type that's not known at runtime. 如果构造该实例的方法应该被继承,那么我看到的唯一方法就是使用Class.newInstance()构造一个在运行时未知的类型的实例。

The problem with that is that within a static method, you have no reference to the containing class, so you don't know whose newInstance method to call. 这样做的问题在于,在静态方法中,您没有对包含类的引用,因此您不知道要调用newInstance方法。

Why do you want to do this, though? 但是,为什么要这样做呢? There may be some better way to achieve whatever it is you want to achieve. 可能有一些更好的方法来实现您想要实现的目标。

In your example I wouldn't put your main method inside of A. This is setup as the entry point into the system (you can't be in B if you are specifically entering into A). 在您的示例中,我不会将您的主要方法放在A内。这是设置为系统的入口点(如果您专门输入A,则不能在B中)。

In the example below I created class A, B, and C. Class C instantiates A and B and runs them. 在下面的示例中,我创建了类A,B和C。类C实例化了A和B并运行它们。 Notice that in CI created an A, a B, and another A that I instantiate as a B. My output is: ABB 注意,在CI中创建了一个A,一个B和另一个实例化为B的A。我的输出是:ABB

Hopefully this makes sense. 希望这是有道理的。

public class A { 
public void show(){ 
    System.out.println("A"); 
  } 

public void run(){ 
    show(); 
  }  
} 

public class B extends A { 
 @Override 
 public void show(){ 
    System.out.println("B"); 
    } 
 } 

public class C {
public static void main(String[] args) {
    A a = new A();
    B b = new B();
    A anothera = new B();

    a.show();
    b.show();
    anothera.show();
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM