从 Java 调用继承的类方法

发布于 2024-08-19 16:20:49 字数 732 浏览 5 评论 0原文

在Python中,类方法可以被继承。例如,

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

您将如何在 Java 中做同样的事情?我目前有:

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");
    }
}

我想调用 B.main() 并让它打印“B”,但显然它会打印“A”,因为“new A()”是硬编码的。

您将如何更改“new A()”,以便将其参数化以使用调用时所在的类,而不是硬编码的 A 类?

In Python, class methods can be inherited. e.g.

>>> 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? 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

忘你却要生生世世 2024-08-26 16:20:49

java 中的静态方法不是类方法,而是静态方法。一般来说,不可能知道静态方法是从哪个类引用调用的。

Static methods in java are not classmethods they are staticmethods. In general it is not possible to know which class reference the static method was called from.

桜花祭 2024-08-26 16:20:49

您的类 B 没有 main 方法,并且静态方法继承。

Your class B does not have a main method and static methods are not inherited.

暮光沉寂 2024-08-26 16:20:49

我看到这种情况发生的唯一方法是找到正在调用 A.main( String[] arg ) 的内容,并将其更改为调用 B.main

B.main:

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

你的程序是如何启动的?有批处理文件、快捷方式等吗?有什么你可以改变的吗? A.main 在哪里被调用?

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.

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?

左秋 2024-08-26 16:20:49

我认为这是不可能的。原因如下:

在 Java 中,方法的实现由实例的运行时类型决定。因此,要执行 B.show(),您需要有一个 B 实例。如果构造实例的方法应该被继承,我能想到的唯一方法是使用 Class.newInstance() 构造运行时未知类型的实例。

问题在于,在静态方法中,您没有对包含类的引用,因此您不知道要调用谁的 newInstance 方法。

但你为什么要这样做呢?可能有更好的方法来实现您想要实现的目标。

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. So, to execute B.show(), you need to have an instance of 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.

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.

Why do you want to do this, though? There may be some better way to achieve whatever it is you want to achieve.

南薇 2024-08-26 16:20:49

在你的例子中,我不会把你的 main 方法放在 A 中。这是设置为系统的入口点(如果你专门进入 A,你就不能在 B 中)。

在下面的示例中,我创建了类 A、B 和 C。C 类实例化 A 和 B 并运行它们。请注意,在 CI 中创建了一个 A、一个 B 和另一个 A,我将其实例化为 B。我的输出是:
一个

B

希望这是有道理的。

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();
   }
}

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).

In the example below I created class A, B, and C. Class C instantiates A and B and runs them. Notice that in C I created an A, a B, and another A that I instantiate as a B. My output is:
A
B
B

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();
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文