如何从子类调用重写的父类方法? [英] How do I call an overridden parent class method from a child class?

查看:387
本文介绍了如何从子类调用重写的父类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个子类,它有从父类重写的方法,并且在非常特殊的情况下我想使用原始方法,我该如何调用这些方法?

If I have a subclass that has methods I've overridden from the parent class, and under very specific situations I want to use the original methods, how do I call those methods?

推荐答案

调用超级

class A {
   int foo () { return 2; }
}

class B extends A {

   boolean someCondition;

   public B(boolean b) { someCondition = b; }

   int foo () { 
       if(someCondition) return super.foo();
       return 3;
   }
}

这篇关于如何从子类调用重写的父类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆