Call a child class method from a parent class object

后端 未结 7 530
星月不相逢
星月不相逢 2020-12-06 09:35

I have the following classes

class Person {
    private String name;
    void getName(){...}}

class Student extends Person{
    String class;
    void getCl         


        
相关标签:
7条回答
  • 2020-12-06 10:16

    A parent class should not have knowledge of child classes. You can implement a method calculate() and override it in every subclass:

    class Person {
        String name;
        void getName(){...}
        void calculate();
    }
    

    and then

    class Student extends Person{
        String class;
        void getClass(){...}
    
        @Override
        void calculate() {
            // do something with a Student
        }
    }
    

    and

    class Teacher extends Person{
        String experience;
        void getExperience(){...}
    
        @Override
        void calculate() {
            // do something with a Student
        }
    
    }
    

    By the way. Your statement about abstract classes is confusing. You can call methods defined in an abstract class, but of course only of instances of subclasses.

    In your example you can make Person abstract and the use getName() on instanced of Student and Teacher.

    0 讨论(0)
  • 2020-12-06 10:17

    NOTE: Though this is possible, it is not at all recommended as it kind of destroys the reason for inheritance. The best way would be to restructure your application design so that there are NO parent to child dependencies. A parent should not ever need to know its children or their capabilities.

    However.. you should be able to do it like:

    void calculate(Person p) {
        ((Student)p).method();
    }
    

    a safe way would be:

    void calculate(Person p) {
        if(p instanceof Student) ((Student)p).method();
    }
    
    0 讨论(0)
  • 2020-12-06 10:18
    class Car extends Vehicle {
            protected int numberOfSeats = 1;
    
            public int getNumberOfSeats() {
                return this.numberOfSeats;
    
            }
    
            public void  printNumberOfSeats() {
              //  return this.numberOfSeats;
                System.out.println(numberOfSeats);
            }
    
    
        } 
    
    //Parent class
    
      class Vehicle {
            protected String licensePlate = null;
    
            public void setLicensePlate(String license) {
                this.licensePlate = license;
                System.out.println(licensePlate);
            }
    
    
       public static void main(String []args) {
           Vehicle c = new Vehicle();
    
          c.setLicensePlate("LASKF12341"); 
    
    //Used downcasting to call the child method from the parent class. 
    //Downcasting = It’s the casting from a superclass to a subclass.
    
          Vehicle d = new Car();
          ((Car) d).printNumberOfSeats();
    
    
       }
       }
    
    0 讨论(0)
  • 2020-12-06 10:22

    Why don't you just write an empty method in Person and override it in the children classes? And call it, when it needs to be:

    void caluculate(Person p){
      p.dotheCalculate();
    }
    

    This would mean you have to have the same method in both children classes, but i don't see why this would be a problem at all.

    0 讨论(0)
  • 2020-12-06 10:27

    I had the same situation and I found a way around with a bit of engineering as follows - -

    1. You have to have your method in parent class without any parameter and use - -

      Class<? extends Person> cl = this.getClass(); // inside parent class
      
    2. Now, with 'cl' you can access all child class fields with their name and initialized values by using - -

      cl.getDeclaredFields(); cl.getField("myfield"); // and many more
      
    3. In this situation your 'this' pointer will reference your child class object if you are calling parent method through your child class object.

    4. Another thing you might need to use is Object obj = cl.newInstance();

    Let me know if still you got stucked somewhere.

    0 讨论(0)
  • 2020-12-06 10:36

    Many of the answers here are suggesting implementing variant types using "Classical Object-Oriented Decomposition". That is, anything which might be needed on one of the variants has to be declared at the base of the hierarchy. I submit that this is a type-safe, but often very bad, approach. You either end up exposing all internal properties of all the different variants (most of which are "invalid" for each particular variant) or you end up cluttering the API of the hierarchy with tons of procedural methods (which means you have to recompile every time a new procedure is dreamed up).

    I hesitate to do this, but here is a shameless plug for a blog post I wrote that outlines about 8 ways to do variant types in Java. They all suck, because Java sucks at variant types. So far the only JVM language that gets it right is Scala.

    http://jazzjuice.blogspot.com/2010/10/6-things-i-hate-about-java-or-scala-is.html

    The Scala creators actually wrote a paper about three of the eight ways. If I can track it down, I'll update this answer with a link.

    UPDATE: found it here.

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