Java: Calling a super method which calls an overridden method

前端 未结 13 2320
有刺的猬
有刺的猬 2020-11-27 10:29
public class SuperClass
{
    public void method1()
    {
        System.out.println(\"superclass method1\");
        this.method2();
    }

    public void method2(         


        
13条回答
  •  一生所求
    2020-11-27 10:57

    I don't believe you can do it directly. One workaround would be to have a private internal implementation of method2 in the superclass, and call that. For example:

    public class SuperClass
    {
        public void method1()
        {
            System.out.println("superclass method1");
            this.internalMethod2();
        }
    
        public void method2()
        {
            this.internalMethod2(); 
        }
        private void internalMethod2()
        {
            System.out.println("superclass method2");
        }
    
    }
    

提交回复
热议问题