从 Java 调用继承的类方法
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
java 中的静态方法不是类方法,而是静态方法。一般来说,不可能知道静态方法是从哪个类引用调用的。
Static methods in java are not
classmethod
s they arestaticmethod
s. In general it is not possible to know which class reference the static method was called from.您的类
B
没有main
方法,并且静态方法不继承。Your class
B
does not have amain
method and static methods are not inherited.我看到这种情况发生的唯一方法是找到正在调用
A.main( String[] arg )
的内容,并将其更改为调用B.main
。B.main:
你的程序是如何启动的?有批处理文件、快捷方式等吗?有什么你可以改变的吗?
A.main
在哪里被调用?The only way I can see this happening is to find whatever is calling
A.main( String[] arg )
and change it to callB.main
instead.B.main:
How is your program started? Is there a batch file, shortcut, etc? Something you can change? Where does
A.main
get called?我认为这是不可能的。原因如下:
在 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 ofB
. The only way I could see to do this, if the method that constructs the instance is supposed to be inherited, is to useClass.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.
在你的例子中,我不会把你的 main 方法放在 A 中。这是设置为系统的入口点(如果你专门进入 A,你就不能在 B 中)。
在下面的示例中,我创建了类 A、B 和 C。C 类实例化 A 和 B 并运行它们。请注意,在 CI 中创建了一个 A、一个 B 和另一个 A,我将其实例化为 B。我的输出是:
一个
乙
B
希望这是有道理的。
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.