简体   繁体   English

在 Java 中如何调用超类中的子类方法?

[英]How do you call subclass method in superclass, in Java?

I am planning on coding an API.我计划编写一个 API。 I want to call a method from the API super class, that will be declared in the class that extends the API class. I want to call a method from the API super class, that will be declared in the class that extends the API class. Is there a way to do this?有没有办法做到这一点? Thanks - PrimeCubed谢谢-PrimeCubed

There is no way.不可能。 Reason is that a subclass is a different object type.原因是子类是不同的 object 类型。 You could try to think about a way to parse your superobject into a subobject.您可以尝试考虑一种将超对象解析为子对象的方法。

Edit: I have no idea of what your API is doing or what so ever.编辑:我不知道您的 API 在做什么或曾经做什么。 If you can, then just post a little more detail.如果可以,那么只需发布更多详细信息。 Usually an extension USES the API.通常一个扩展使用 API。 So if you think in plugins, then you offer the basic structure and use interfaces to create general objects.因此,如果您考虑插件,那么您将提供基本结构并使用接口来创建通用对象。

Maybe events are a solution for you?也许事件是您的解决方案?

If you want more concrete hints, then please post more details.如果您想要更具体的提示,请发布更多详细信息。

Edit 2: Check out abstract classes and interfaces if you are trying to create an API.编辑 2:如果您尝试创建 API,请查看抽象类和接口。 That is a very good way to go.这是 go 的一个非常好的方法。 And even further, the observer pattern could be useful for registering some listeners etc.更进一步,观察者模式可能对注册一些听众等有用。

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

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