内部类中的方法可以访问父类的方法吗?

发布于 2024-11-02 03:11:14 字数 526 浏览 6 评论 0原文

我不确定我的问题标题是否恰当地描述了我的情况,如果没有,我很抱歉!无论如何,假设我有以下代码片段(可见性如所述):

public class ChildClass extends ParentClass {
    // more code
   private void myMethod() {
      MyClass mine = new MyClass() {
         public void anotherMethod() {
            // insert code to access a method in ParentClass
         }
      };
   }
}

anotherMethod() 中的代码是否可以访问 ParentClass 中找到的受保护方法?如果是这样,该怎么办?

我尝试过类似的方法...

(ParentClass.this).parentMethod();

...但显然由于范围问题它不起作用。

I'm not sure if my question title describes my situation aptly, so my apologies if it doesn't! Anyway, let's say I have the following code snippet (visibility is as stated):

public class ChildClass extends ParentClass {
    // more code
   private void myMethod() {
      MyClass mine = new MyClass() {
         public void anotherMethod() {
            // insert code to access a method in ParentClass
         }
      };
   }
}

Is it possible for code within anotherMethod() to access a protected method found in ParentClass? If so, how can this be done?

I've tried something like...

(ParentClass.this).parentMethod();

...but obviously it doesn't work due to scope issues.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

为你鎻心 2024-11-09 03:11:14

这编译得很好:

class MyClass {
}

class ParentClass {
    protected void parentMethod() {
    }
}

class ChildClass extends ParentClass {
    private void myMethod() {
        MyClass mine = new MyClass() {
            public void anotherMethod() {
                parentMethod(); // this works
            }
        };
    }
}

This compiles fine:

class MyClass {
}

class ParentClass {
    protected void parentMethod() {
    }
}

class ChildClass extends ParentClass {
    private void myMethod() {
        MyClass mine = new MyClass() {
            public void anotherMethod() {
                parentMethod(); // this works
            }
        };
    }
}
桜花祭 2024-11-09 03:11:14

非静态内部类可以访问封闭类的所有方法,就好像它是它自己的方法一样:

public class Test {
    public int getOne() {
        return 1;
    }

    public class Inner {
        public int getEnclosingOne() {
            return getOne(); // this works...
        }
    }
}

静态内部类不能,因为静态内部类不绑定到父类的实例。这只能调用封闭类上的静态方法。

至于考虑继承的方法,非静态内部类中的方法可以使用封闭(外部)类的所有方法。

有趣的部分是 Test2.super.getOne() ,它确实从 Test2.super 获取 getOne() ,这是一个 Test 。这就像 Test2 访问该方法一样,即使用 super,尽管前缀为 Test2 来指示您正在访问外部类的名称空间。

public class Test2 extends Test {

    public int getInnerOuterParentOne() {
        Inner2 inner2 = new Inner2();
        return inner2.getOuterParentOne();
    }
    public int getInnerOuterOne() {
        Inner2 inner2 = new Inner2();
        return inner2.getOuterOne();
    }

    public int getOne() {
        return 2;
    }

    public class Inner2 {
        public int getOuterOne() {
            return getOne();
        }
        public int getOuterParentOne() {
            return Test2.super.getOne();
        }
    }

    public static void main(String[] args) {
        Test2 test2 = new Test2();
        System.out.println(test2.getInnerOuterOne()); // 2
        System.out.println(test2.getInnerOuterParentOne()); // 1
    }
}

A non-static inner class can access all methods of the enclosing class as if it were it's own methods:

public class Test {
    public int getOne() {
        return 1;
    }

    public class Inner {
        public int getEnclosingOne() {
            return getOne(); // this works...
        }
    }
}

A static inner class can not, as a static inner class is not bound to an instance of the parent class. That can only call static methods on the enclosing class.

As for methods when taking into account inheritance, an method in a non-static inner class can use all the methods of the enclosing (outer) class.

The interesting part is Test2.super.getOne() which indeed obtains getOne() from Test2.super, which is a Test. This is just like Test2 would access the method, namely using super though prefixed with Test2 to indicate you're accessing the namespace of the outer class.

public class Test2 extends Test {

    public int getInnerOuterParentOne() {
        Inner2 inner2 = new Inner2();
        return inner2.getOuterParentOne();
    }
    public int getInnerOuterOne() {
        Inner2 inner2 = new Inner2();
        return inner2.getOuterOne();
    }

    public int getOne() {
        return 2;
    }

    public class Inner2 {
        public int getOuterOne() {
            return getOne();
        }
        public int getOuterParentOne() {
            return Test2.super.getOne();
        }
    }

    public static void main(String[] args) {
        Test2 test2 = new Test2();
        System.out.println(test2.getInnerOuterOne()); // 2
        System.out.println(test2.getInnerOuterParentOne()); // 1
    }
}
小姐丶请自重 2024-11-09 03:11:14

Java 中无法访问“父类方法”,与可见性无关(子类的 parentMethod() 中的 super.parentMethod() 除外)。

也就是说,如果 ChildClass 重写 parentMethod(),则无法调用 ParentClass.parentMethod() (绕过 ChildClass.parentMethod ()) 来自 ChildClass 的其他方法。

但是,如果 ChildClass 不重写 parentMethod(),则该方法将由 ChildClass 继承,以便您可以将其作为 访问>ChildClass 的方法,即简单为 parentMethod()

There is no way to access "parent class method" in Java, irrelatively to visibility (except for super.parentMethod() in subclass's parentMethod()).

That is, if ChildClass overrides parentMethod(), there is no way to call ParentClass.parentMethod() (bypassing ChildClass.parentMethod()) from other methods of ChildClass.

However, if ChildClass doesn't override parentMethod(), that method is inherited by ChildClass, so that you can access it as a ChildClass's method, i.e. simply as parentMethod().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文