Get child class name from parent

前端 未结 6 584
广开言路
广开言路 2021-02-06 21:36

I have a base class for my all of activities (ActivityBase) that itself derives from android.app.Activity. In onCreate I want to execute some conditio

相关标签:
6条回答
  • 2021-02-06 22:01

    On some occasions simply this line in the parent class solves this problem. It returns the name of the "child" class (not the parent):

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

    See here for further discussion: http://www.coderanch.com/t/324715/java/java/Getting-child-class-name-parent

    0 讨论(0)
  • 2021-02-06 22:02

    Use instanceof operator.

    Supposing you have a base class and two subclasses named Base, SubOne and SubTwo, if you want to check if a variable ref is an instance of SubOne or SubTwo you'd say:

    if(ref instanceof SubOne){
    }
    else if(ref instanceof SubTwo){
    }
    

    Note that: (ref instanceof Base) will always return true though.

    0 讨论(0)
  • 2021-02-06 22:09

    I think you want to use the instanceof operator, for example:

    if(this instanceof SomeCustomActivity) {
        // do stuff
    } else if (this instanceof AnotherCustomActivity) {
        // do other stuff
    }
    

    And that is all there is to it.

    0 讨论(0)
  • 2021-02-06 22:12

    Instead of using an if statement you should create an abstract method for your conditional logic. Then have the child class run the code that is correct for it.

    This also will keep you from having to modify the base class every time you create a new child class.

    0 讨论(0)
  • 2021-02-06 22:24

    In your super class create method for the condition which is responsible for answering question - is the sub class one of the type X. In the processing logic use this method to decide which code block to execute. Each sub class can override decision method and answer as needed. As result your super class has no knowledge about sub classes and sub classes don't have to worry about the actual processing implementation.

    abstract class A {
    
        abstract boolean isItX();
    
        void doX() { ... }
    
        void doY() { ... }
    
        void process() {
            if (isItX()) {
                doX();
            } else {
                doY();
            }
        }
    }
    
    class B extends A {
        boolean isItX() {
            return true;
        }
    }
    
    class C extends A {
        boolean isItX() {
            return false;
        }
    }
    

    For more information see Hollywood Principle.

    0 讨论(0)
  • 2021-02-06 22:27

    You can also use the .getClass() method of the parent and then check like this

    if(parent.getClass().equals(childObj.class)){
    //to do ..
    }
    

    This basically works because it returns the runtime object class. This will especially work in case of abstract classes - Tried and tested recently by me in my project.

    0 讨论(0)
提交回复
热议问题