java - 从父类获取子类名

标签 java android inheritance

我的所有 Activity (ActivityBase) 都有一个基类,它本身派生自 android.app.Activity。在 onCreate 中,我想根据当前正在执行的子类执行一些条件逻辑。如果 SomeCustomActivityAnotherCustomActivity 都扩展了 ActivityBase,我如何在父类 (ActivityBase) 中确定哪个二是当前正在执行的?

最佳答案

在某些情况下,父类中的这一行就可以解决这个问题。它返回“子”类(不是父类)的名称:

this.getClass().getName() //String like "com.mycompany.myclassname"
this.getClass().getSimpleName() //String like "myclassname"

更多讨论请看这里:http://www.coderanch.com/t/324715/java/java/Getting-child-class-name-parent

关于java - 从父类获取子类名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5176294/

相关文章:

java - 递归java问题

Android Studio 预期的类或包错误

java - 如何将对象传递给android studio中的新 Activity ?

android - 在 IntelliJ IDEA 12 中使用 Facebook Android SDK 时似乎没有获取 Facebook SDK 资源

java - 将数据从 select 标签发送到 servlet

java - WebMvcAutoConfiguration 未激活

java - Spring MVC Controller 测试 - 打印结果 JSON 字符串

ios - 覆盖 UITableViewController 子类中的 cellForRowAtIndexPath 返回错误 : does not override any method from superclass

javascript - 在 NodeJS 中实现接口(interface)

python - 从命名元组基类继承