Call a child class method from a parent class object

后端 未结 7 561
星月不相逢
星月不相逢 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: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.

提交回复
热议问题